0

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?

CornerH
  • 11
  • 4
  • In Bash 5.0.7 it works, maybe getopts don't works in Ksh like in Bash – A.Villegas Dec 13 '19 at 09:34
  • Thanks..does it work with the ksh shebang "#!/usr/bin/ksh" in Bash 5.0,7? As I already mentioned, it works if I change the shebang to bash. I am not sure what is the change I have to make to make it work with "#!/usr/bin/ksh". – CornerH Dec 13 '19 at 09:43
  • 1
    Looks like the spaces mess things up. [Got it running](https://tio.run/##VY@7EoIwEEX7fMUabSwYGiso/AorhyKPFTKYhGFxVH4@hhQh3uLO3HO22ZGGEI6H@kVzLY2rRxoYew/midDj4qeFgOvG0iokhzhb0J6hGjyc4mIQowRhWmBcAls0nCGdcc2hbTO3mVthnrwwlA05O5VmzebhZ4VN6cTuqOQyc/mdBP25a3Y3Ej02APfKdrFoq7UDvt9uwY9Z4BK/AJY4klClZ9o7DCFU4gc) without them – Aaron Dec 13 '19 at 09:46
  • 1
    Wow! Yes, space it is. Thanks brother! – CornerH Dec 13 '19 at 09:49
  • @ceving run by [Dennis](https://codegolf.stackexchange.com/users/12012/dennis), one of the members of the [Code Golf SE](https://codegolf.stackexchange.com/). I usually prefer [ideone](https://ideone.com/), but [TryItOnline](https://tio.run) supports much more languages, especially esoteric ones. – Aaron Dec 13 '19 at 10:25

1 Answers1

0

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

CornerH
  • 11
  • 4