Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use <code>getopts</code>, but I don't like it because it's complicated to use and it doesn't support long option names (not the POSIX version anyway).</p> <p>I recommend against using environment variables. There's just too much risk of name collision. For example, if your script reacts differently depending on the value of the <code>ARCH</code> environment variable, and it executes another script that (unbeknownst to you) also reacts to the <code>ARCH</code> environment variable, then you probably have a hard-to-find bug that only shows up occasionally.</p> <p>This is the pattern I use:</p> <pre class="lang-sh prettyprint-override"><code>#!/bin/sh usage() { cat &lt;&lt;EOF Usage: $0 [options] [--] [file...] Arguments: -h, --help Display this usage message and exit. -f &lt;val&gt;, --foo &lt;val&gt;, --foo=&lt;val&gt; Documentation goes here. -b &lt;val&gt;, --bar &lt;val&gt;, --bar=&lt;val&gt; Documentation goes here. -- Treat the remaining arguments as file names. Useful if the first file name might begin with '-'. file... Optional list of file names. If the first file name in the list begins with '-', it will be treated as an option unless it comes after the '--' option. EOF } # handy logging and error handling functions log() { printf '%s\n' "$*"; } error() { log "ERROR: $*" &gt;&amp;2; } fatal() { error "$*"; exit 1; } usage_fatal() { error "$*"; usage &gt;&amp;2; exit 1; } # parse options foo="foo default value goes here" bar="bar default value goes here" while [ "$#" -gt 0 ]; do arg=$1 case $1 in # convert "--opt=the value" to --opt "the value". # the quotes around the equals sign is to work around a # bug in emacs' syntax parsing --*'='*) shift; set -- "${arg%%=*}" "${arg#*=}" "$@"; continue;; -f|--foo) shift; foo=$1;; -b|--bar) shift; bar=$1;; -h|--help) usage; exit 0;; --) shift; break;; -*) usage_fatal "unknown option: '$1'";; *) break;; # reached the list of file names esac shift || usage_fatal "option '${arg}' requires a value" done # arguments are now the file names </code></pre>
    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. 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