Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ getopt(1) — A/UX 2.0

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

csh(1)

ksh(1)

sh(1)

getopt(3C)




getopt(1) getopt(1)
NAME getopt - parse command options SYNOPSIS getopt [flag-letter[:]]... [input-string] DESCRIPTION getopt returns input-string with additional separators to help distinguish any flag options, any arguments associated with the flag options, and any arguments not associated with the flag options. By replacing input-string with the command arguments $* for a script, getopt helps shell scripts to parse their command-line arguments by making a regularized copy of them as well as checking them for legal options. The regularization that getopt can perform for each flag op- tion is twofold or threefold: 1. Each flag option on the command line is returned separated with white space. 2. Each flag option on the command line is returned with a leading hyphen. 3. Optionally, the argument associated with a given flag is returned with white space. Each flag-letter helps control how input-string is manipu- lated to detect flags and flag arguments. If a flag-letter is followed by a : (colon), getopt expects to find a flag- specific argument following that flag in the input-string. For example, getopt a: $* requires that -a always be followed by its own argument (ei- ther with or without a space separator), as in the follow- ing: yourcommand -a param ... yourcommand -aparam ... The special option -- can be used within input-string to re- quest that only a portion of input-string actually be pro- cessed for the presence of flags. Any text following -- is not processed. If it is not supplied explicitly, getopt still generates the symbol in its output to help separate any flags and flag arguments found from any nonflag argu- ments that might remain in input-string. For example, getopt abo: $* April, 1990 1



getopt(1) getopt(1)
returns -a -o param -- xxxx yyyy zzzz when you place the getopt command line (shown above) in a command script invoked with yourcommand -aoparam xxxx yyyy zzzz Even though a hyphen was not specified in front of each flag option in this example, the output of getopt includes hy- phens in front of both a and o. To reset the shell's positional parameters ($1 $2...) so that they are regularized by getopt and so that each discrete flag and flag argument is stored as a unique posi- tional parameter, specify the output of getopt as the argu- ment for set by using command substitution: set -- `getopt abo: $*` Quoted Arguments getopt correctly parses quoted arguments within input- string. However, if the input string you wish to parse with getopt is specified as $* in order to request the parsing of command-line arguments, any quotes that may be present in the command line are automatically stripped by the shell. In such cases you need to use a reference to the unstripped version of the command-line arguments, $@. For example getopt a:b: "$@" correctly returns -a 'hello world' -b oneword -- when the getopt command line (shown above) is in a script invoked with yourcommand -a'hello world' -b oneword The challenge then becomes resetting the shell's positional parameters so that 'hello world' is interpreted as one posi- tional argument rather than two positional arguments ('hello as one argument and world' as another). To do so, use eval to invoke the set function, as in the following: eval set -- `getopt abo: "$@"` To preserve the opportunity to process the exit status of 2 April, 1990



getopt(1) getopt(1)
getopt, the eval command line cannot be used as shown preceding. (The exit status from getopt is lost when eval is used to evaluate a command string.) The only recourse is to defer the resetting of positional arguments until after the exit status stored in the $? vari- able can be tested: x=`getopt abo: "$@"` if [ $? != 0 ] then echo $USAGE exit 2 fi eval set -- $x A nonzero exit value conventionally indicates that process- ing was terminated abnormally. So in the example preceding, the value of the exit status variable is used to detect whether or not the string processing performed by getopt succeeded: which in turn depends on whether or not getopt recognized and regularized the input string in terms of the control arguments supplied. EXAMPLES The following code fragment shows how one might process the arguments for a command that can take the options a or b, as well as the option o, which requires an argument. x=`getopt abo: "$@"` if [ $? != 0 ] then echo $USAGE exit 2 fi eval set -- $x for i in "$@" do case $i in -a | -b) FLAG=$i; shift;; -o) OARG=$2; shift 2;; --) shift; break;; esac done If this code is placed in a script called cmd, then any of the following invocations are accepted as equivalent: cmd -aoarg file1 file2 cmd -a -oarg file1 file2 cmd -o arg -a file1 file2 cmd -a -oarg -- file1 file2 April, 1990 3



getopt(1) getopt(1)
The script also interprets any imbedded blanks in arguments correctly, as long as the arguments are quoted as in the following: cmd -aoarg "file one" "file two" cmd -a -o "an arg" "file two" "file two" cmd -o "an arg" -a "file one" "file two" cmd -a -o"an arg" -- "file two" "file two" FILES /bin/getopt SEE ALSO csh(1), ksh(1), sh(1), getopt(3C). DIAGNOSTICS getopt prints an error message on the standard error when it encounters an option letter not included as a flag-letter. 4 April, 1990

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026