getopts fails

From: Edwin R Wolfe Jr. <ewolfe_at_umich.edu>
Date: Tue, 09 Mar 1999 17:09:50 -0500 (EST)

I can't seem to get getopts to work in DU 4.0D, 4.0C, or 4.0. I copied
the example from the man page into a file I named zzz:

            aflag=
            bflag=
            while getopts ab: name
            do
                   case $name in
                 a) aflag=1;;
                 b) bflag=1
                     bval="$OPTARG";;
                 ?) printf "Usage: %s: [-a] [-b value] args\n" $0
                     exit 2;;
                 esac
            done
            if [ ! -z "$aflag" ]; then
                 printf "Option -a specified\n"
            fi
            if [ ! -z "$bflag" ]; then
                 printf 'Option -b "%s" specified\n' "$bval"
            fi
            shift $(($OPTIND - 1))
            printf "Remaining arguments are: %s\n" "$*"


xxx -a begat the error:
zzz: syntax error at line 21: `(' unexpected
which is the line with the shift command. commenting that out gave no
errors, but it failed to indicate the specified option.

I tried specifying #!/usr/bin/sh with the same result. Specifying
the explicit path /usr/bin/getopts changed nothing. Using #!/usr/bin/csh
gave the errors
aflag=: Command not found.
bflag=: Command not found.
while: Expression syntax.
 
Using #!ksh (not mentioned in the man page) made it work (even
uncommenting the line with the shift command) - as long as I left it with
getopts instead of /usr/bin/getopts. (the explicit path gave no error, it
just didn't report the argument).

Can someone familiar with scripts enlighten me as to just what's going on
here? Surely there must be a way to use this in a sh script?
Received on Tue Mar 09 1999 - 22:12:33 NZDT

This archive was generated by hypermail 2.4.0 : Wed Nov 08 2023 - 11:53:39 NZDT