Dear all,
I have created a KornShell script containing swiches with getopts (command line switches). Normally, my script should work like this:
$ ./myscript.ksh -a 12 -b 4 -c 78
The switch a was selected with the argument 12
The switch b was selected with the argument 4
The switch c was selected with the argument 78
$
$
$ ./myscript.ksh -a 12 -b 4 -c
The switch a was selected with the argument 12
The switch b was selected with the argument 4
CURRENT_SWITCH = c : error : missing argument
Now the problem is that when I don't supply an argument for the switch -c, the script
considers the precedent switch as the current switch that is CURRENT_SWITCH=b
instead of CURRENT_SWITCH=c. So here is what I obtain:
$ ./myscript.ksh -a 12 -b 4 -c
The switch a was selected with the argument 12
The switch b was selected with the argument 4
CURRENT_SWITCH = b : error : missing argument
Could somebody kindly guide me where did I make a mistake in my implementation?
Here is the script code:
#!/bin/ksh
CURRENT_SWITCH=""
while getopts :a:b:c: SWITCHES
do
case $SWITCHES in
a) CURRENT_SWITCH="a"
print -n "The switch $CURRENT_SWITCH was selected with "
print "the argument $OPTARG"
;;
b) CURRENT_SWITCH="b"
print -n "The switch $CURRENT_SWITCH was selected with "
print "the argument $OPTARG"
;;
c) CURRENT_SWITCH="c"
print -n "The switch $CURRENT_SWITCH was selected with "
print "the argument $OPTARG"
;;
:) print -n "CURRENT_SWITCH = $CURRENT_SWITCH : error : "
print "missing argument"
exit 1
;;
\?) print "unknown switch"
exit 1
;;
esac
done
Thanks in advance,