Search code examples
bashcentos7kshgetopts

I am running a korn script (ksh) on a Cent Os 7 bash shell. The "getopts" is not working as expected. What am I doing wrong?


The problem script

testcmd.ksh

#!/usr/bin/ksh

while getopts "d: m s z a b " opt; do
echo $opt
    case $opt in
        d ) echo "d" ;;
        m ) echo "mail";;
        s ) echo "snmp";;
        z ) echo "force:";;
        a ) echo "fs";;
        b ) echo "bypass";;
        ? ) echo "Usage:  [-m] [-s] [-z] ";
            exit 4 ;; 

    esac            
done    

if i run the above script as "./testcmd.ksh -a", it gives

./testcmd.ksh: -a: unknown option
?
Usage:  [-m] [-s] [-z] 

The output I was expecting was something like this

a
fs

What could be the problem?

If I change the shebang to "#!/usr/bin/bash", it works as expected.

But, I have to use "#!/usr/bin/ksh" only.

What is the change I should do? I am using CentOs 7.

Could someone please advise?


Solution

  • Just to help anyone who is looking for an answer:

    The answer that worked is the comment by Aaron:

    Looks like the spaces mess things up. Got it running without them – Aaron