Results 1 to 4 of 4

Thread: Help with simple shell program

  1. #1
    Just burned his ISO
    Join Date
    Mar 2006
    Posts
    21

    Default Help with simple shell program

    I am trying to write a program in shell to replace strings in a text file. I'm pretty sure that my method is right, I just think I am not declaring / calling my variables correctly.
    Code:
    #!/bin/tcsh
    
    
    #calling variables
    @ replacements=0
    @ i=0
    set file
    set string[index]
    set replacement[index]
    
    
    #Introduction
    echo "This script will substitute a pattern with another one."
    echo "How many strings will you be replacing?"
    $replacements
    echo "What is the path to the file you want to modify?"
    $file
    cp $file $file.saved
    
    #The Meat
    while ( $i<= $replacements)
    echo "Enter $i pattern."
    $string[$i]
    echo "Enter what you want pattern $i replaced with"
    $replacement[$i]
    sed 's/$string[$i]/$replacement[$x]/g' $file
    $i++
    end

  2. #2
    Senior Member shamanvirtuel's Avatar
    Join Date
    Mar 2010
    Location
    Somewhere in the "Ex" human right country
    Posts
    2,988

    Default

    if you want to get an input use read

    echo -n "What is the path to the file you want to modify?" (-n avoid to pass a line before input)
    read file (no dollar)
    echo
    echo "The Path is $file"

    but you should have a look to getops and add option switch to your command line like -f file

    more clean

    touch me in irc if you need advise
    ..
    Watch your back, your packetz will belong to me soon... xD

    BackTrack :
    Giving Machine Guns to Monkeys since 2006

  3. #3
    Junior Member drpepperONE's Avatar
    Join Date
    Nov 2007
    Posts
    51

    Default

    Hi Little_Dice this is an example of getopts usage

    Code:
        usage () {
        echo -e “Usage: $0 -e \”What To Echo\” [ -q \”Output\” ]”
        echo -e “\t-e: specifies what to echo (required)”
        echo -e “\t-q: Where to write output to. If not specified the output is written to the console”
        }
    
        while getopts “:e:q:” ARG;
        do case “${ARG}” in
        e) ECHO=”${OPTARG}”;;
        q) OUTPUT=”${OPTARG}”;;
        esac;
        done
    
        [ -z “${ECHO}” ] && { usage && exit 1; }
    
        [ “${OUTPUT}” ] && { echo $ECHO > $OUTPUT; } || { echo $ECHO; }
    Use your Brain, don't be slave!!!!

  4. #4
    My life is this forum thorin's Avatar
    Join Date
    Jan 2010
    Posts
    2,629

    Default

    Why re-invent the wheel?
    Code:
    vi somefile
    :%s/foo/bar/g
    :wq
    Or even easier:
    Code:
    sed -i "s/foo/bar/g" somefile
    Both of which replace all occurrences of the string "foo" with the string "bar" in the file "somefile".
    I'm a compulsive post editor, you might wanna wait until my post has been online for 5-10 mins before quoting it as it will likely change.

    I know I seem harsh in some of my replies. SORRY! But if you're doing something illegal or posting something that seems to be obvious BS I'm going to call you on it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •