It is currently Tue, 17 May 2022 18:57:03 GMT



 
Author Message
 Nested ctrl statements syntax confusion

I get easily confused when nesting the various control constructs in
bash shell scripts.

Here we have an `while' inside an `if' inside a `case' inside a
`while'.
  (and the thigh bones connected to the knee bone...)

Probably should be easy to get the format right, but not for me. Not
all is shown here.  For breivity I've snipped a couple of `here
documents' and commments.  When I try to run this with the "-w" flag
and 3 args.    -w(1) "something"(2)  non-file(3)

(using a non-file name to test)

I get this error... :

line 71: syntax error near unexpected token `;'
line 71: `          done                      ;;    '

[I've rearranged the double colons the fis and the
dones many times and the error message changes accordingly,
but having a problem seeing my mistake]

I'd like some advice on how to debug this kind of problem easily.

Also a related syntactical question:  In the `if' clause I've
attempted to capture the argument count:
 ` cnt="$#"'
And presented it for a file test:
 if     [ -f "$($cnt)" ] ;then

Trying to determine if last arg is infact a file.
But not sure if this is how that would be done.

Pseudo script:
^^^^^^^^^^^^^^^^^^^^^^
[`Here docs' snipped [...] various usage statements]

What is wrong in this picture?

This script, with -w flag expects the user to insert data and hit ^d to write
it to the selected file.

    [ -z "$1" ] && {
       usage1
       exit 1
   }    
      while getopts "hHw:r:" opt; do
       case "$opt" in
        w) cnt="$#"
   ## wants to see if last arg is legit filename
        if      [ -f "$($cnt)" ] ;then
   ## save OPTARG for writing
           wdata=`echo "Something: $OPTARG"`
           shift
           echo "$wdata"         >>   $1
           date +"%b %d %T %Y"   >>   $1
           echo  "Hit ^d when done to write data to $2"

           while read line
               do
               echo $line        >>  $1            
           done                       ;;                

        else
           echo ""
           echo "WAIT...$cnt either doesn't exist or is not a regular file"
           echo "type \`$(basename $0) -h' for minimal usage statement"
           echo "type \`$(basename $0) -H' for maximum usage statement"
           echo ""
           exit 1                    
      fi
        r) awk '/^Keywords:.*'"$OPTARG"'/,/\&\&$/' $3 ;;      
        h) usage1                     ;;
        H) usage2                     ;;
        *)      usage1                     ;;

           esac
   done



 Mon, 02 Jun 2003 22:07:55 GMT   
 Nested ctrl statements syntax confusion

The case pattern terminating ";;" doesn't go into the middle of
an embedded "while" statement, as "done ;;" above; it goes after
the "fi" and just before following case patterns: "fi ;;".

As for your second query, the shell parses variables expansion once
only; to make the test conditional work, change 'cnt="$#"' to "eval
lastfile=\${$#}" (double-quotes are not necessary.) Variable
"lastfile" will now contain the value of the last positional
parameter, which you may test on directly.

-Brian



 Tue, 03 Jun 2003 04:42:28 GMT   
 Nested ctrl statements syntax confusion

[...]

Well put... he he... I had discovered exactly that with considerable
more fiddling before I saw your post... thanks.

Not sure I follow this.  Are you saying that $@ and relatives  don't
hold the same value at the end of the script as they do at the
beginning?

Before seeing your post, I'd altered the script quite a bit to solve
this and some other problems and ended up with this technique to set
up the file test:
 w) last_arg=`echo $@|awk '{print $NF}'`

     if [ -f "$last_arg" ] ;then
[...]
Which seems to work too.

Now working with your new information, and I'm always pleased to see
examples of the sometimes intricate escape/quoting some times
required, and the different usages of () and {}. Seems like quite a
lot to keep up with sometimes.

I'd probably never have hit on \${$#}

So trying your example like this:

  ## capture the value of the last command line arg
       w) "eval lastfile=\${$#}"

  ## Test to see if it is a legit file    
         if     [ -f "$lastfile"  ] ;then

Kicks out an error that can be seen in the "-x" flag output:

$ tasks2 -w "now" it

[...]

   + getopts hHw:r: opt
   + 'eval lastfile=${3}'
   /home/reader/scripts/tasks2: eval lastfile=${3}: command not found
   + '[' -f '' ']'

[...]

Am I missing a quote somewhere or maybe need to supply a `path' for
`eval'.



 Tue, 03 Jun 2003 06:30:06 GMT   
 Nested ctrl statements syntax confusion
In article <m2elzahc0h....@gnus-5.8.8-cvs.now.playing>,
Harry Putnam  <rea...@newsguy.com> wrote:

There are a number of commands that modify what $@ and relatives hold:
shift moves $2 into $1, $3 into $2, and so on; getopts updates them to
remove the option parameters, and the "set" command can be used to replace
the whole parameter list.

--
Barry Margolin, bar...@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.



 Wed, 04 Jun 2003 08:24:00 GMT   
 
   [ 4 post ] 

Similar Threads

1. nested case statement ??

2. nesting 'case' statements

3. Syntax error in nested loop?

4. LILO append statement syntax

5. LILO append statement syntax

6. Conditional statement syntax in a Makefile

7. syntax for an IF statement

8. syntax of if statement

9. Help with if then statement and loop statement

10. cannot change resolution using Ctrl-Alt+KP_- and Ctrl-Alt+KP_+


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software