Problem with getopts in ksh
Brad Appleton
brad at SSD.CSD.HARRIS.COM
Wed Jun 13 00:28:48 AEST 1990
Okay - thanx to the net I switched to getopts instead of getopt. I am still
having a problem though (although not quite as serious). Page 216 of the
Korn Shell Manual states that:
A leading ':' in <optstring> affects the behavior of getopts when
getopts encounters an option letter not in< optstring>. If <optstring>
begins with ':', getopts puts the letter in OPTARG and sets <name> to
'?' for an unknown option, and sets <name> to ':' when a required option
arguments is ommitted. Otherwise, getopts displays an error message and
sets <name> to '?'.
So now I have the following shellscript:
#!/bin/ksh
#
# tst -- test getopts
#
options="ac:d:"
argv="-a -b -c arg -f -d"
print "Pass 1: options=$options argv=$argv"
while getopts $options OPT $argv
do case $OPT in
a) print "a_flag=SET" ;;
c) print "c_flag=$OPTARG" ;;
d) print "d_flag=$OPTARG" ;;
*) print -u2 "invalid option $OPT" ;;
esac
done
print "Pass 2: options=$options argv=$argv"
while getopts ":$options" OPT $argv
do case $OPT in
a) print "a_flag=SET" ;;
c) print "c_flag=$OPTARG" ;;
d) print "d_flag=$OPTARG" ;;
:) print -u2 "$OPTARG requires an argument" ;;
\?) print -u2 "invalid option $OPTARG" ;;
esac
done
Now, in pass 1, when I have an invalid option - I would expect to see
"invalid option ?" printed to the screen (unless getopts returns FALSE
for bad options syntax)
However, even if getopts does return FALSE for bad syntax, I would still
expect the \? and : cases of pass 2 to be performed, and based on my
output, this does not appear to be the case (the output listed below is
with the -x option turned on to enable tracing):
+ options=ac:d:
+ argv=-a -b -c arg -f -d
+ print Pass 1: options=ac:d: argv=-a -b -c arg -f -d
Pass 1: options=ac:d: argv=-a -b -c arg -f -d
+ getopts ac:d: OPT -a -b -c arg -f -d
+ print a_flag=SET
a_flag=SET
+ getopts ac:d: OPT -a -b -c arg -f -d
tst[11]: getopts: bad option(s)
+ print Pass 2: options=ac:d: argv=-a -b -c arg -f -d
Pass 2: options=ac:d: argv=-a -b -c arg -f -d
+ getopts :ac:d: OPT -a -b -c arg -f -d
+ print c_flag=arg
c_flag=arg
+ getopts :ac:d: OPT -a -b -c arg -f -d
+ getopts :ac:d: OPT -a -b -c arg -f -d
+ print d_flag=
d_flag=
+ getopts :ac:d: OPT -a -b -c arg -f -d
Am I doing something wrong (I hope so) or is this a bug in /bin/ksh on
my system? The version of ksh I am running is 11/16/88 (or at least thats
what the "what" command tells me).
advTHANXance
______________________ "And miles to go before I sleep." ______________________
Brad Appleton brad at ssd.csd.harris.com Harris Computer Systems
...!{uunet,novavax}!hcx1!brad Fort Lauderdale, FL USA
~~~~~~~~~~~~~~~~~~~~ Disclaimer: I said it, not my company! ~~~~~~~~~~~~~~~~~~~
More information about the Comp.unix.questions
mailing list