shell script: cannot shift error?

This is an assignment where we were supposed to create a script to get an orginal string and replace it with another. However when I run my script (change-lines), it says

./change-lines: cannot shift

I do not where the problem is. help!

#!/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 the 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;

---------- Post updated at 06:51 PM ---------- Previous update was at 02:25 PM ----------

cannot shift.................

There were couple of syntax errors... No shift error though...
Try this...

...
if [ ! "$search_string" -a ! "$replace_string" ]
then
   error_and_die_with_usage "No Search or Replace String entered."
fi
...

# here is where I am pretty sure I am getting the error.
for file in $*
do
        backup=$file
        if [ $backup_file == "TRUE" ]
        then
                cp $backup $backup.keep
        fi
        sed "s/$search_string/$replace_string/g" $backup
        #shift <<<< You don't need this here now
done

--ahamed

PS : Please use code tags. Thank You.

First off, you have a quote missing in the statement

error_and_die_with_usage "No Search or Replace String entered.

That isn't your problem, but might be causing other odd things to happen.

Yes, the loop you indicate is where the problem is. Since this is homework, I will only point out the fact that you might consider using a while loop, rather than a for loop, or reference $file in the loop and drop the shift. That may seem confusing at first, but give it some thought and you'll figure it out.

Thanks ahamad and agama. I just used the for loop because it was there already and since I am using bash it was giving me an unknown == operator error but I changed that to just = and it worked! great.

Just a question though - why did doing a " around sed instead of the ' work?
Thanks a lot.

Cause " allows your variables to expand inside sed without extra work, while when enclosed in ' they will be taken as is.

Example :


$ A=a; export A;
$ echo "a" | sed "s/$A/b/g"
b
$ echo "a" | sed 's/$A/b/g'
a
$ echo "a" | sed 's/'$A'/b/g'
b
$