Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are essentially two versions of the <code>getopt</code> command: the original version and the GNU enhanced version. The GNU enhanced version is backward compatible with the original version, so if you only use the features of the original version it will work with both.</p> <p><strong>Detect which version of getopt is available</strong></p> <p>You can detect which version is available and use the enhanced features if the GNU enhanced version is available, and limit yourself to the original features if the GNU enhanced version is not available. The enhanced version has a <code>-T</code> option for testing which version is available.</p> <pre><code>getopt -T &gt; /dev/null if [ $? -eq 4 ]; then # GNU enhanced getopt is available set -- `getopt --long help,output:,version --options ho:v -- "$@"` else # Original getopt is available set -- `getopt ho:v "$@"` fi </code></pre> <p>Consider using built-in shell command <code>getopts</code> (with an "s") instead, because it is more portable. However, <code>getopts</code> does not support long options (e.g. <code>--help</code>).</p> <p>If you like long options, use <code>getopt</code> and use the above test to see if the GNU enhanced version of <code>getopt</code> is available or not. If the enhanced version is not available, the script can gracefully degrade to either using the original version of <code>getopt</code> (with no support for long option names and no whitespace support) or using <code>getopts</code> (with no support for long option names).</p> <p><strong>Using GNU enhanced getopt properly</strong></p> <p>Getting the GNU enhanced version to process arguments with whitespace properly is tricky. Here's how it is done:</p> <pre><code>ARGS=`getopt --long help,output:,verbose --options ho:v -- "$@"` if [ $? -ne 0 ]; then echo "Usage error (use -h for help)" &gt;&amp;2 exit 2 fi eval set -- $ARGS # Parameters are now sorted: options appear first, followed by --, then arguments # e.g. entering: "foo bar" -o abc baz -v # produces: -o 'abc' -v -- 'foo bar' 'baz' </code></pre> <p>The secret is to use <code>"$@"</code> where the double quotes are very important (in line 1), and to <code>eval</code> the <em>set</em> command (in line 6).</p> <p>So errors raised by <code>getopt</code> can be detected and handled, the call to <code>getopt</code> is done separately from the <code>eval</code> with the two linked by the ARGS variable.</p> <p><strong>Complete working example</strong></p> <pre><code>PROG=`basename $0` getopt -T &gt; /dev/null if [ $? -eq 4 ]; then # GNU enhanced getopt is available ARGS=`getopt --name "$PROG" --long help,output:,verbose --options ho:v -- "$@"` else # Original getopt is available (no long option names, no whitespace, no sorting) ARGS=`getopt ho:v "$@"` fi if [ $? -ne 0 ]; then echo "$PROG: usage error (use -h for help)" &gt;&amp;2 exit 2 fi eval set -- $ARGS while [ $# -gt 0 ]; do case "$1" in -h | --help) HELP=yes;; -o | --output) OUTFILE="$2"; shift;; -v | --verbose) VERBOSE=yes;; --) shift; break;; # end of options esac shift done if [ $# -gt 0 ]; then # Remaining parameters can be processed for ARG in "$@"; do echo "$PROG: argument: $ARG" done fi echo "$PROG: verbose: $VERBOSE" echo "$PROG: output: $OUTFILE" echo "$PROG: help: $HELP" </code></pre> <p>This example can be downloaded from <a href="https://gist.github.com/hoylen/6607180" rel="noreferrer">https://gist.github.com/hoylen/6607180</a></p> <p>The comparison table on <a href="http://en.wikipedia.org/wiki/Getopts" rel="noreferrer">Wikipedia's entry on getopts</a> compares the different features.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload