I need to use getopts
sooner (and more often) in Bourne shell scripts
I have this flaw in my Bourne shell scripting; in shell scripts and shell scripts alone, I find it all too easy and tempting to reach for ad-hoc option processing when I add the first option to a script. Inevitably, handling the first option will look something like:
[...] SOMEVAR=0 if [ "$1" = "-L" ]; then SOMEVAR=1; shift fi [...]
When a second option shows up, it sometimes winds up getting wedged awkwardly into the same sort of code. Maybe the options conflict so they'll never get used together in practice, or I'll tell myself that I'll always remember the order, or some equally bad excuse.
Doing this is a mistake, and I need to accept that. Rather than ever
writing ad-hoc option processing code, I should be using getopts
in my scripts right from the start even in tiny little scripts. Even
using getopts
for a single option is not that much more code over the
simple version above, and it has better error handling. And of course,
adding more options is much simpler and works much better if I'm using
getopts
already.
(Probably I should just memorize and master a standard getopts
stanza so I can drop it into everything by reflex. But that's going
to take time, and also not reflexively reaching for the quick
solution for single options.)
I'm not entirely sure why I'm so reluctant in practice to use
getopts
, but for whatever reason it's not part of my shell landscape
most of the time. This is silly, as getopts
has existed for a
long time and is a Single Unix Specification standard.
As a practical matter it's long since been supported everywhere
that I'm ever going to run a shell script. I need to get with the times
and update my Bourne shell habits.
(This entry is brought to you by me updating some scripts today,
trying to add a second option to them in a terrible way, and then
telling myself that I should be doing this right and switching them
over to using getopts
. The resulting shell script code is both
better and shorter.)
Comments on this page:
|
|