shell script: cannot shift error?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script: cannot shift error?
# 1  
Old 10-29-2011
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!
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 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.................


Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by Franklin52; 10-31-2011 at 06:11 AM.. Reason: Please use code tags, thank you
# 2  
Old 10-29-2011
There were couple of syntax errors... No shift error though...
Try this...
Code:
...
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.

Last edited by ahamed101; 10-30-2011 at 12:05 AM.. Reason: Grammatic
# 3  
Old 10-30-2011
First off, you have a quote missing in the statement

Code:
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.
# 4  
Old 10-30-2011
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.

Last edited by alis; 10-30-2011 at 01:19 AM..
# 5  
Old 10-30-2011
Cause " allows your variables to expand inside sed without extra work, while when enclosed in ' they will be taken as is.

Example :
Code:
$ 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
$

This User Gave Thanks to Peasant For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calling shell script within awk script throws error

I am getting the following error while passing parameter to a shell script called within awk script. Any idea what's causing this issue and how to ix it ? Thanks sh: -c: line 0: syntax error near unexpected token `newline' sh: -c: line 0: `./billdatecalc.sh ... (10 Replies)
Discussion started by: Sudhakar333
10 Replies

2. Homework & Coursework Questions

Need help with a Perl Script using Pop, Shift, & Push

Hello everyone, I am new to Perl and I am having some issues getting a script to work. I have to create a script that uses an array of 52 cards, "shuffles" the cards (using loops with the pop, shift, and push commands), and prints out the top five. This is not a randomizing of the array just a... (2 Replies)
Discussion started by: Hax0rc1ph3r
2 Replies

3. UNIX for Dummies Questions & Answers

can someone explain shift command in script?

think using shift would help me finish my script but cant get it work without your help. would appreciate if you give me a example with shift & counter in the same script so I can later work on that to my one. Thanks and Good Luck! (1 Reply)
Discussion started by: me.
1 Replies

4. Shell Programming and Scripting

Error in calling a shell script from another script

HI, We are using two shell scripts, script.sh,env.sh, where env.sh will be called inside script.sh. The variable inside env.sh is used as $var in script.sh.But while running the script its not identifying that variable. Is there any permission needed to call a script inside another script. ... (3 Replies)
Discussion started by: banupriyat
3 Replies

5. Shell Programming and Scripting

Syntax error calling TCL script from shell script

hello everyone i am beginner on shell scripting .and i am working on my project work on ad hoc network i wrote a batch (.sh) to do a looping and execute a tcl script i wrote before in each iteration ..but i got this problem " syntax error near unexpected token `('... (1 Reply)
Discussion started by: marcoss90
1 Replies

6. Shell Programming and Scripting

Shift report script

hey guys, so i'm running into a wall here with my script. i simply can't figure out a way to get it to work. so, maybe you guys can help me. i'm trying to created a report of server alerts based on the time worked. what i have so far is curling nagios pages, removing all the extra html tags and... (4 Replies)
Discussion started by: terrell
4 Replies

7. Shell Programming and Scripting

how to shift few words of filenames at a time using shell script

Hello everybody, I have some files in directory. I want to shift 3 characters of filenames to the right at a same time. for example, I have filenames like $ls -l 01_2000.G3.input.txt 02_2000.G3.input.txt ..., ..., 04_2010.G3.input.txt I want to change the filenames like... (3 Replies)
Discussion started by: yogeshkumkar
3 Replies

8. Shell Programming and Scripting

How to grep sql error in shell script and exit the script?

I need help in the following script. I want to grep the sql errors insert into the error table and exit the shell script if there is any error, otherwise keep running the scripts. Here is my script #!/bin/csh -f source .orapass set user = $USER set pass = $PASS cd /opt/data/scripts echo... (2 Replies)
Discussion started by: allinshell99
2 Replies

9. Shell Programming and Scripting

script assistance with shift J

Hey all, I need some assistance. I'm writing a script to eject tapes from a tape library, but the library is not a queued system and can only eject 15 tapes at a time. I added paste -d : -s so that it goes through full_tapes and puts each media_id on one line separated by the :. Now I'm... (2 Replies)
Discussion started by: em23
2 Replies

10. UNIX for Dummies Questions & Answers

awk Shell Script error : "Syntax Error : `Split' unexpected

hi there i write one awk script file in shell programing the code is related to dd/mm/yy to month, day year format but i get an error please can anybody help me out in this problem ?????? i give my code here including error awk ` # date-month -- convert mm/dd/yy to month day,... (2 Replies)
Discussion started by: Herry
2 Replies
Login or Register to Ask a Question