Question

I'm testing getopt for my bourne script and I'm finding some very interesting results.

#!/bin/sh

params=$(getopt lLo:p:t: "$*")
exit 0

If I do the following:

./myscript -z blah -o legal -p another_legal

As expected I will get:

getopt: illegal option -- z
getopt: illegal option --
getopt: illegal option -- b
getopt: illegal option -- l
getopt: illegal option -- a
getopt: illegal option -- h

But if I do this:

./myscript -o legal -p another_legal -z blah 

getopt does not catch this. Any ideas why?

Was it helpful?

Solution

"$*" passes all arguments your function received as a single argument to getopt. So you're essentially running getopt lLo:p:t: "-o legal -p another..."You want "$@" instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top