Re: [Help-bash] Allow getopts to do what is capable with getopt?
From:
Greg Wooledge
Subject:
Re: [Help-bash] Allow getopts to do what is capable with getopt?
Date:
Thu, 26 Mar 2015 08:14:43 -0400
User-agent:
Mutt/1.4.2.3i
On Wed, Mar 25, 2015 at 03:55:38PM -0600, Bob Proulx wrote:
> I think that is too harsh. It is perfectly reasonable to use getopt
> in portable shell scripts. I have great respect for your deep
> knowledge of the shell but I disagree that getopt should never be used
> in shell scripts.
Even the HP-UX 10.20 man page says this:
WARNINGS
getopt option arguments must not be null strings nor contain embedded
blanks.
So, if you use getopt then you can never receive a filename, nor any
kind of arbitrary user-controlled string, as an option argument.
imadev:~$ set -- -v -f "my file"
imadev:~$ eval set -- "$(getopt vf: "$@")"
imadev:~$ printf '<%s> ' "$@"; echo
<-v> <-f> <my> <file> <-->
And, even worse! You can't even accept filenames as regular arguments
AFTER the processed options.
imadev:~$ set -- -v -- "my file"
imadev:~$ eval set -- "$(getopt vf: "$@")"
imadev:~$ printf '<%s> ' "$@"; echo
<-v> <--> <my> <file>
If you don't agree that this makes it completely unsuitable for any real
life work, then I don't know how else to convince you.
So, I was wrong about it being called multiple times in a loop. That's
because I would never ever use it, so I wasn't immediately familiar
with its typical invocation.