[olug] Option processing in scripts

William E. Kempf wekempf at cox.net
Mon Dec 16 18:29:30 UTC 2002


I'm writing a lot of Bash scripts for which I need to do extensive command
line option processing.  My first question is about the differences
between the built-in 'getopts' and the 'getopt' program.  Most sites that
talk about the differences seem to indicate that the built-in 'getopts' is
superior and should be the first choice, but that seems completely wrong
to me since 'getopt' can handle long options.  One site seems to indicate
that 'getopt' can't handle options/arguments with spaces properly, but
that doesn't seem to be the case in practice.  So what do people on here
use, and why?

The second question... I've mostly settled on using 'getopt' for now, but
I can't seem to figure out how to allow '-?' as a short option for
printing usage.  It seems that as soon as 'getopt' encounters a -? it
terminates with no output and a return value of 1.  Unfortunately, if the
user supplies an unknown option we also get a return value of 1, so I'm
not sure how to distinguish this (valid) option from an error.  I've no
clue why 'getopt' behaves this way and would consider it a bug, but what's
important to me is whether or not there's a work around.  Anyone have any
ideas?

William E. Kempf






More information about the OLUG mailing list