Problem in understanding debugging | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Problem in understanding debugging

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-11-2013
scriptor scriptor is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 21 August 2014, 7:05 AM EDT
Posts: 96
Thanks: 10
Thanked 0 Times in 0 Posts
Problem in understanding debugging

Hi

i was going through the script debugging technique. below example was given in the book.

Code:
1   #!/bin/sh
     2
     3   Failed() {
     4   if [ $1 -ne 0 ] ; then
     5   echo "Failed. Exiting." ; exit 1 ;
     6   fi
     7   echo "Done."
     8   }
     9
    10   echo "Deleting old backups, please wait... \c"
    11   rm -r backup > /dev/null 2>&1
    12   Failed $?
    13
    14   echo "Make backup (y/n)? \c"
    15   read RESPONSE
    16   case $RESPONSE in
    17   [yY]|[Yy][Ee][Ss]|*)
    18   echo "Making backup, please wait... \c"
    19   cp -r docs backup
    20   Failed
    21   [nN]|[Nn][Oo])
    22   echo "Backup Skipped." ;;
    23  esac

my problem is when author debug the script with -nv option he is getting below o/p

Code:
[nN]|[Nn][Oo]) ./buggy2.sh: syntax error at line 21: ')'
unexpected

however when i debug the same script with same option i am getting below o/p

Code:
./debug6: 21: Syntax error: ")" unexpected (expecting ";;")

in my case i just change the name of the script.(i use debug6 and author used buggy2.sh rest i simply copy paste the script.)
why here i am getting bit more info in compare to author's o/p. is there is some problem or it is ok.

to correct the script author suggested to put (; ; after Failed in line no. 20 like below.

Code:
Failed ;;

my question is why i need to use 2 colon after Failed not one?
also how does author know just by seeing one single line(means by seeing o/p of debug) that he need to put 2 colon after Failed.
Sponsored Links
    #2  
Old 02-11-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 22 November 2014, 4:22 PM EST
Location: Amsterdam
Posts: 9,606
Thanks: 293
Thanked 2,445 Times in 2,193 Posts
That is simply a matter of syntax of the case statement. By the way this:

Code:
[yY]|[Yy][Ee][Ss]|*)

is wrong, since it is equivalent to

Code:
*)

Sponsored Links
    #3  
Old 02-11-2013
scriptor scriptor is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 21 August 2014, 7:05 AM EDT
Posts: 96
Thanks: 10
Thanked 0 Times in 0 Posts
Hi Scrutinizer

the code is working.
Code:
[yY]|[Yy][Ee][Ss]|*)

my below question is still unanswered.


Code:
my question is why i need to use 2 colon after Failed not one?
also how does author know just by seeing one single line(means by seeing o/p of debug) that he need to put 2 colon after Failed.

is this is also due to case statement ?
    #4  
Old 02-11-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 22 November 2014, 4:22 PM EST
Location: Amsterdam
Posts: 9,606
Thanks: 293
Thanked 2,445 Times in 2,193 Posts
Sure it is working, it is just that it is wrong. Whatever you input, it will be interpreted as "yes".

Your first question I answered.

The author sees that the shell is not expecting to see a ")" character and the author concludes that this is due to the fact that the previous part was not closed with ;; , which is a matter of syntax.
Sponsored Links
    #5  
Old 02-11-2013
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Registered User
 
Join Date: Mar 2012
Last Activity: 9 October 2014, 4:50 PM EDT
Location: India
Posts: 1,412
Thanks: 101
Thanked 496 Times in 473 Posts
That's two semi-colons and not colons.

Because that's the syntax for terminating a block of case statements. The first semi-colon ends that particular statement and the second ends the whole case block. The second one ensures that you don't try the following case blocks.

It is also possible to try the following block/s if you replace the second semi-colon with an ampersand (I think that's called a fall-through).

Code:
var='THE UNIX AND LINUX FORUMS';

case "$var" in

 *UNIX*)   echo "Has UNIX";&

 *LINUX*)  echo "Has LINUX";;

 *FORUMS*) echo "HAS FORUMS";;

esac

produces

Code:
Has UNIX
Has LINUX

Sponsored Links
    #6  
Old 02-11-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 22 November 2014, 4:22 PM EST
Location: Amsterdam
Posts: 9,606
Thanks: 293
Thanked 2,445 Times in 2,193 Posts
;& is bash 4.0. The OP states #!/bin/sh
Sponsored Links
    #7  
Old 02-11-2013
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Registered User
 
Join Date: Mar 2012
Last Activity: 9 October 2014, 4:50 PM EDT
Location: India
Posts: 1,412
Thanks: 101
Thanked 496 Times in 473 Posts
Quote:
Originally Posted by Scrutinizer View Post
;& is bash 4.0. The OP states #!/bin/sh
Oops, I didn't see the shebang line. By the way, it's not bash-specific; also works in ksh88/93.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Problem in understanding export uses scriptor Shell Programming and Scripting 25 07-11-2012 11:29 PM
Problem understanding Paths Chasman78 UNIX for Dummies Questions & Answers 10 05-26-2012 04:41 PM
Problem with the shell script for understanding nixhead Shell Programming and Scripting 6 03-18-2011 07:45 AM
debugging problem satish@123 Solaris 2 05-30-2008 06:50 AM
egrep understanding problem namishtiwari Shell Programming and Scripting 1 01-29-2008 07:03 AM



All times are GMT -4. The time now is 10:00 PM.