help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Bash script supporting complex options like GNU find


From: Greg Wooledge
Subject: Re: [Help-bash] Bash script supporting complex options like GNU find
Date: Mon, 12 Feb 2018 08:44:17 -0500
User-agent: NeoMutt/20170113 (1.7.2)

On Sat, Feb 10, 2018 at 11:42:53AM -0600, Peng Yu wrote:
> I currently use getopt to support -s --long like options in bash scripts.
> 
> https://github.com/karelzak/util-linux/blob/master/misc-utils/getopt-parse.bash

I would not advise doing this.

Oh, you need *reasons*?  OK, here's the top one: the getopt(1) software
you are using is a nonstandard GNU command which acts ENTIRELY DIFFERENTLY
from the getopt(1) command that is commonly present on Unix systems.
Anyone attempting to run your script on a Unix-that-is-not-GNU system
will encounter the (broken) traditional behavior.  Not only will your
--long-options NOT work, but any argument containing a space, or any
empty argument, will just completely break.


>From the HP-UX (11.11) getopt(1) man page:

 WARNINGS
      getopt option arguments must not be null strings nor contain embedded
      blanks.


>From the OpenBSD getopt(1) at <https://man.openbsd.org/getopt>:

CAVEATS
  Note that the construction set -- `getopt optstring $*` is not
  recommended, as the exit value from set will prevent the exit value
  from getopt from being determined.

BUGS
  Whatever getopt(3) has.

  Arguments containing whitespace or embedded shell metacharacters
  generally will not survive intact; this looks easy to fix but isn't.

  The error message for an invalid option is identified as coming from
  getopt rather than from the shell procedure containing the invocation
  of getopt; this again is hard to fix.

  The precise best way to use the set command to set the arguments without
  disrupting the value(s) of shell options varies from one shell version
  to another.


> But this solution does not support complex expressions made of '-a'
> '-o'  '!' '(' and ')' as in defined in GNU find.
> 
> Does anybody know a solution that can make a bash script support these
> complex expressions?

Yes: write your own option processing code.

Here's another general tidbit of advice: if you find yourself wondering
how to construct an abstract syntax tree in a bash script, you probably
shouldn't be writing this project in bash.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]