Why getopts doesn't detect correctly my switches?

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,
:slight_smile:

Remove the first colon:

while getopts a:b:c: SWITCHES

You used CURRENT_SWITCH in your output, which was still set to the last valid option. If there's an invalid option it's name will be set in OPTARG.

#!/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 = $OPTARG : error : "
            print "missing argument"
            exit 1
            ;;
            
        \?)    print "unknown switch" 
            exit 1
            ;;
    esac
done

Thanks a lot, yes in fact my mistake was that I had to look for the last token in OPTARG and not in CURRENT_SWITCH. Now it works pretty well!

Thanks again for your help.

Kind Regards,
:slight_smile: