Find the answer to your Linux question:
Results 1 to 5 of 5
I'm trying to write what is basically a search and replace script. Full disclosure: This IS a homework assignment, and my attempt is below. It is supposed to have a ...
  1. #1
    Just Joined!
    Join Date
    Mar 2009
    Posts
    3

    Search and Replace Function (homework)

    I'm trying to write what is basically a search and replace script. Full disclosure: This IS a homework assignment, and my attempt is below. It is supposed to have a usage statement, which I think is pretty solid so far. The problem is at the end where I do that actual searching and replacing. I'm just getting the 'end of file unexpected' error, which because i am spoiled in my young programming life with gcc, g++, and java compilers that neatly tell you where the problem is, I don't really know what is going wrong.

    I think that error means it is looking for something it can't find, right? What?

    Thanks in advance if anyone helps.




    Code:
    #!/bin/sh
    
    # a shell function to print and error message and exit the script
    error_and_die ()
        {
        echo "$@" >&2
        exit 1
    
        }
    # a shell function to print and error message and a usage message and
    # to exit the script
    error_and_die_with_usage ()
        {
        echo "$@" >&2
        usage
        exit 1
        }
    
    # a shell function to print a usage message
    usage ()
    {
    echo "
    change-lines [-n] -s search string -r replace string files ...
    
        -n    do not backup the original file
        -s    search string the search for this string
        -r    replace string replace the search string with this string
        -h    print this message
    "  >&2
    }
    
    
    backup_file=TRUE
    search_string=
    replace_string=
    
    while :
        do
        case $1 in
        -n)
           backup_file=FALSE
           ;;
        -s)
            if [ ! "$2" ]
                then
                error_and_die_with_usage "Search String not specified with -s"
                fi
            search_string="$2"
            shift 
            ;;
        -r)
            if [ ! "$2" ]
                then
                error_and_die_with_usage "Search String not specified with -r"
                fi
            replace_string="$2"
            shift 
            ;;
        -h)
            usage
            exit 0
            ;;
        -?)
            error_and_die_with_usage "This should help!"
            ;;
        *)
            break
            ;;
        esac
        shift
        done
    
    if [ ! "$search_string" -a ! "$replace_string"  -a $# -eq 0 ]
        then
        error_and_die_with_usage "No Search String, Replace String, or file to edit entered."
        fi
    
    if [ ! "$search_string" -a ! "$replace_string" ]
        then
        error_and_die_with_usage "No Search or Replace String entered.
        fi
    
    if [ ! "$search_string" -a $# -eq 0 ]
        then
        error_and_die_with_usage "No Search String or file to edit entered."
        fi
    
    if [ ! "$search_string" ]
        then
        error_and_die_with_usage "No Search String entered."
        fi
    
    if [ ! "$replace_string"  -a $# -eq 0 ]
        then
        error_and_die_with_usage "No Replacement String or file to edit entered."
        fi
    
    if [ ! "$replace_string" ]
        then
        error_and_die_with_usage "No Replacement String entered."
        fi
    
    if [ $# -eq 0 ]
        then
        error_and_die_with_usage "No file to edit entered."
        fi
    
    # here is where I am pretty sure I am getting an error.
    for file in $*
        do
                    backup=$1
                    if ( backup_file == TRUE )
                    then
                          cp $backup $backup.keep
                    fi
                    sed 's/$search_string/$replace_string/g' $backup
                    shift
        done
    
            exit 0;
    Last edited by MikeTbob; 06-16-2010 at 04:02 AM. Reason: Added code tags

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    One shell debugging tool is
    Code:
    set -x
    place such a line after the shebang line, and see if that helps.

    If you need to post more code, please use CODE tags: select the text with the mouse and then click the # above the editing window ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  3. #3
    Just Joined!
    Join Date
    Mar 2009
    Posts
    3

    That was perfect.

    That is exactly what I needed. I know it's a simple tip, but thank you very much for it. I haven't fixed it yet, but at least I'm not in a black hole staring it down.

  4. #4
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    To get the most debugging information you can use:

    Code:
    set -vx
    If you don't want to put that in your script you can specify it when you run the script:

    Code:
    sh -vx ./script

  5. #5
    Linux Newbie
    Join Date
    Oct 2008
    Posts
    140
    You can set the -x in the first line too.
    Code:
    #!/bin/bash -vx
    On a related note, what do the plus signs indicate?

    Code:
     echo "Do you want to input a fraction of an hour or a whole number?"
    + echo 'Do you want to input a fraction of an hour or a whole number?'
    Do you want to input a fraction of an hour or a whole number?
      echo "f or w?"
    + echo 'f or w?'
    f or w?
       read RESPONSE
    + read RESPONSE
    This part works, but I don't understand how to interpret it. The plus signs don't appear in the if loop, though.

Posting Permissions

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