Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uflorida!hcx1!brad From: brad@SSD.CSD.HARRIS.COM (Brad Appleton) Newsgroups: comp.unix.questions Subject: Problem with getopts in ksh Summary: getopts doesnt seem to handle bad options as documented Keywords: getopts, ksh, Korn Shell Message-ID: <4455@hcx1.UUCP> Date: 12 Jun 90 14:28:48 GMT References: <1990Jun11.105027.16511@gdr.bath.ac.uk> Sender: news@hcx1.UUCP Organization: Harris Computer Systems, Fort Lauderdale, FL Lines: 82 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 affects the behavior of getopts when getopts encounters an option letter not in< optstring>. If begins with ':', getopts puts the letter in OPTARG and sets to '?' for an unknown option, and sets to ':' when a required option arguments is ommitted. Otherwise, getopts displays an error message and sets 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@ssd.csd.harris.com Harris Computer Systems ...!{uunet,novavax}!hcx1!brad Fort Lauderdale, FL USA ~~~~~~~~~~~~~~~~~~~~ Disclaimer: I said it, not my company! ~~~~~~~~~~~~~~~~~~~