Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ getopt(1) — A/UX 3.0.1

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

csh(1)

ksh(1)

sh(1)

getopt(3C)




getopt(1) getopt(1)
NAME getopt - parses command options SYNOPSIS getopt [flag-letter[:]]... [input-string] ARGUMENTS flag-letter[:] Helps control how input-string is manipulated 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 (either with or without a space separator), as in the following: yourcommand -a param ... yourcommand -aparam ... input-string Specifies the input string to be parsed. The special option -- can be used within input-string to request that only a portion of input-string actually be processed 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 options and arguments found from any nonflag arguments that might remain in input-string. For example, getopt abo: $* 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 option in this example, the output of getopt includes hyphens in front of both a and o. DESCRIPTION getopt returns input-string with additional separators to help distinguish any options, any arguments associated with January 1992 1



getopt(1) getopt(1)
the options, and any arguments not associated with the 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 option is twofold or threefold: 1. Each option on the command line is returned separated with white space. 2. Each option on the command line is returned with a leading hyphen. 3. Optionally, the argument associated with a given is returned with white space. 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 positional parameter, specify the output of getopt as the argument 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, $@, which is available in the sh and ksh shells. 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 positional 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: 2 January 1992



getopt(1) getopt(1)
eval set -- `getopt abo: "$@"` To preserve the opportunity to process the exit status of 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 $? variable can be tested: x=`getopt abo: "$@"` if [ $? != 0 ] then echo $USAGE exit 2 fi eval set -- $x A nonzero exit value conventionally indicates that processing 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 if 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 January 1992 3



getopt(1) getopt(1)
cmd -a -oarg file1 file2 cmd -o arg -a file1 file2 cmd -a -oarg -- file1 file2 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" DIAGNOSTICS The getopt command prints an error message on the standard error when it encounters an option letter not included as a flag-letter. FILES /bin/getopt Executable file SEE ALSO csh(1), ksh(1), sh(1) getopt(3C) in A/UX Programmer's Reference 4 January 1992

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