GETOPTS(1) | User Commands | GETOPTS(1) |
getopts - parse utility options
/usr/bin/getopts optstring name [arg...]
getopts optstring name [argument]...
getopts optstring name [arg]...
getopts [-a name] optstring name [arg]...
The getopts utility can be used to retrieve options and option-arguments from a list of parameters.
Each time it is invoked, the getopts utility places the value of the next option in the shell variable specified by the name operand and the index of the next argument to be processed in the shell variable OPTIND. Whenever the shell is invoked, OPTIND is initialized to 1.
When the option requires an option-argument, the getopts utility places it in the shell variable OPTARG. If no option was found, or if the option that was found does not have an option-argument, OPTARG is unset.
If an option character not contained in the optstring operand is found where an option character is expected, the shell variable specified by name is set to the question-mark ( ? ) character. In this case, if the first character in optstring is a colon (:, the shell variable OPTARG is set to the option character found, but no output is written to standard error; otherwise, the shell variable OPTARG is unset and a diagnostic message is written to standard error. This condition is considered to be an error detected in the way arguments were presented to the invoking application, but is not an error in getopts processing.
If an option-argument is missing:
When the end of options is encountered, the getopts utility exits with a return value greater than zero; the shell variable OPTIND is set to the index of the first non-option-argument, where the first −− argument is considered to be an option-argument if there are no other non-option-arguments appearing before it, or the value $# + 1 if there are no non-option-arguments; the name variable is set to the question-mark character. Any of the following identifies the end of options: the special option −−, finding an argument that does not begin with a −, or encountering an error.
The shell variables OPTIND and OPTARG are local to the caller of getopts and are not exported by default.
The shell variable specified by the name operand, OPTIND and OPTARG affect the current shell execution environment.
If the application sets OPTIND to the value 1, a new set of parameters can be used: either the current positional parameters or new arg values. Any other attempt to invoke getopts multiple times in a single shell execution environment with parameters (positional parameters or arg operands) that are not the same in all invocations, or with an OPTIND value modified to be a value other than 1, produces unspecified results.
getopts is a built-in Bourne shell command used to parse positional parameters and to check for valid options. See sh(1). It supports all applicable rules of the command syntax standard (see Rules 3-10, Intro(1)). It should be used in place of the getopt command.
optstring must contain the option letters the command using getopts recognizes. If a letter is followed by a colon, the option is expected to have an argument, or group of arguments, which must be separated from it by white space.
Each time it is invoked, getopts places the next option in the shell variable name and the index of the next argument to be processed in the shell variable OPTIND. Whenever the shell or a shell script is invoked, OPTIND is initialized to 1.
When an option requires an option-argument, getopts places it in the shell variable OPTARG.
If an illegal option is encountered, ? is placed in name.
When the end of options is encountered, getopts exits with a non-zero exit status. The special option - can be used to delimit the end of the options.
By default, getopts parses the positional parameters. If extra arguments (argument ...) are specified on the getopts command line, getopts parses them instead.
/usr/lib/getoptcvt reads the shell script in filename, converts it to use getopts instead of getopt, and writes the results on the standard output.
So that all new commands adhere to the command syntax standard described in Intro(1), they should use getopts or getopt to parse positional parameters and check for options that are valid for that command.
getopts prints an error message on the standard error when it encounters an option letter not included in optstring.
Although the following command syntax rule (see Intro(1)) relaxations are permitted under the current implementation, they should not be used because they can not be supported in future releases of the system. As in the EXAMPLES section below, -a and -b are options, and the option -o requires an option-argument.
The following example violates Rule 5: options with option-arguments must not be grouped with other options:
example% cmd -aboxxx filename
The following example violates Rule 6: there must be white space after an option that takes an option-argument:
example% cmd -ab oxxx filename
Changing the value of the shell variable OPTIND or parsing different sets of arguments can lead to unexpected results.
Checks arg for legal options. If arg is omitted, the positional parameters are used. An option argument begins with a + or a −. An option not beginning with + or − or the argument - ends the options. optstring contains the letters that getopts recognizes. If a letter is followed by a :, that option is expected to have an argument. The options can be separated from the argument by blanks.
getopts places the next option letter it finds inside variable name each time it is invoked with a + prepended when arg begins with a +. The index of the next arg is stored in OPTIND. The option argument, if any, gets stored in OPTARG.
A leading : in optstring causes getopts to store the letter of an invalid option in OPTARG, and to set name to ? for an unknown option and to : when a required option is missing. Otherwise, getopts prints an error message. The exit status is non-zero when there are no more options.
getopts supports both traditional single-character short options and long options defined by Sun's Command Line Interface Paradigm (CLIP).
Each long option is an alias for a short option and is specified in parentheses following its equivalent short option. For example, you can specify the long option file as an alias for the short option f using the following script line:
getopts "f(file)" opt
Precede long options on the command line with -- or ++. In the example above, --file on the command line would be the equivalent of -f, and ++file on the command line would be the equivalent of +f.
Each short option can have multiple long option equivalents, although this is in violation of the CLIP specification and should be used with caution. You must enclose each long option equivalent parentheses, as follows:
getopts "f:(file)(input-file)o:(output-file)"
In the above example, both --file and --input-file are the equivalent of -f, and --output-file is the equivalent of -o.
The variable name is always set to a short option. When a long option is specified on the command line, name is set to the short-option equivalent.
For a further discussion of the Korn shell's getopts built-in command, see the previous discussion in the Bourne shell (sh) section of this manpage.
The getopts utility can be used to retrieve options and arguments from a list of arguments specified by args or the positional parameters if arg is omitted. It can also generate usage messages and a manual page for the command based on the information in optstring.
Each time it is invoked, the getopts utility places the value of the next option in the shell variable specified by the name operand and the index of the next argument to be processed in the shell variable OPTIND. When the shell is invoked OPTIND is initialized to 1. When an option requires or permits an option argument, getopts places the option argument in the shell variable OPTARG. Otherwise OPTARG is set to 1 when the option is set and 0 when the option is unset.
The optstring string consists of alphanumeric characters, the special characters +, -, ?, :, and SPACE or character groups enclosed in [...]. Character groups can be nested in {...}. Outside of a [...] group, a single NEWLINE followed by zero or more blanks is ignored. One or more blank lines separate the options from the command argument synopsis.
Each [...] group consists of an optional label, optional attributes separated by :, and an optional description string following ?. The characters from the ? to the end of the next ] are ignored for option parsing and short usage messages. They are used for generating verbose help or man pages. The : character can not appear in the label. The ? character must be specified as ?? in the label and the ] character must be specified as ]] in the description string. Text between two \b (backspace) characters indicates that the text should be emboldened when displayed. Text between two \a (bell) characters indicates that the text should be emphasized or italicized when displayed. Text between two \v (vertical tab) characters indicates that the text should displayed in a fixed-width font. Text between two \f (form feed) characters is replaced by the output from the shell function whose name is that of the enclosed text.
All output from this interface is written to the standard error.
There are several group types:
[-[version][flag[number]]...[?text]]
which appears as the first group enables the extended interface.
version specifies the interface version, currently 1. The latest version is assumed if version is omitted. Future enhancements can increment version, but all versions are supported. text typically specifies an SCCS or CVS identification string. Zero or more flags with optional number values can be specified to control option parsing. The flags are:
c
i
l
o
p
s
If the leading character of optstring is +, arguments beginning with + are also be considered options.
A leading : character or a : following a leading + in optstring affects the way errors are handled. If an option character or longname argument not specified in optstring is encountered when processing options, the shell variable whose name is name is set to the ? character. The shell variable OPTARG is set to the character found. If an option argument is missing or has an invalid value, then name is set to the : character and the shell variable OPTARG is set to the option character found. Without the leading :, name is set to the ? character, OPTARG is unset, and an error message is written to standard error when errors are encountered.
The end of options occurs when:
If OPTIND is set to the value 1, a new set of arguments can be used.
getopts can also be used to generate help messages containing command usage and detailed descriptions. Specify args as:
-?
--??
--??man
--??api
--??html
--??nroff
--??usage
--???name
When the end of options is encountered, getopts exits with a non-zero return value and the variable OPTIND is set to the index of the first non-option argument.
The following options are supported by ksh93:
-a name
The following operands are supported:
optstring
name
The getopts utility by default parses positional parameters passed to the invoking shell procedure. If args are specified, they are parsed instead of the positional parameters.
Since getopts affects the current shell execution environment, it is generally provided as a shell regular built-in. If it is called in a subshell or separate utility execution environment, such as one of the following:
(getopts abc value "$@")
nohup getopts ...
find . -exec getopts ... \;
it does not affect the shell variables in the caller's environment.
Notice that shell functions share OPTIND with the calling shell even though the positional parameters are changed. Functions that want to use getopts to parse their arguments usually want to save the value of OPTIND on entry and restore it before returning. However, there are cases when a function wants to change OPTIND for the calling shell.
Example 1 Parsing and Displaying Arguments
The following example script parses and displays its arguments:
aflag= bflag= while getopts ab: name do
case $name in
a) aflag=1;;
b) bflag=1
bval="$OPTARG";;
?) printf "Usage: %s: [-a] [-b value] args\n" $0
exit 2;;
esac done if [ ! -z "$aflag" ]; then
printf "Option -a specified\n" fi if [ ! -z "$bflag" ]; then
printf 'Option -b "%s" specified\n' "$bval" fi shift $(($OPTIND - 1)) printf "Remaining arguments are: %s\n" "$*"
Example 2 Processing Arguments for a Command with Options
The following fragment of a shell program processes the arguments for a command that can take the options -a or -b. It also processes the option -o, which requires an option-argument:
while getopts abo: c do
case $c in
a | b) FLAG=$c;;
o) OARG=$OPTARG;;
\?) echo $USAGE
exit 2;;
esac done shift `expr $OPTIND − 1`
Example 3 Equivalent Code Expressions
This code example accepts any of the following as equivalent:
cmd -a -b -o "xxx z yy" filename cmd -a -b -o "xxx z yy" -- filename cmd -ab -o xxx,z,yy filename cmd -ab -o "xxx z yy" filename cmd -o xxx,z,yy -b -a filename
See environ(7) for descriptions of the following environment variables that affect the execution of getopts: LANG, LC_ALL, LC_CTYPE, LC_MESSAGES, and NLSPATH.
OPTIND
OPTARG
The following exit values are returned:
0
>0
The following exit values are returned by ksh93:
0
1
2
See attributes(7) for descriptions of the following attributes:
ATTRIBUTE TYPE | ATTRIBUTE VALUE |
Interface Stability | Committed |
Standard | See standards(7). |
ATTRIBUTE TYPE | ATTRIBUTE VALUE |
Interface Stability | Uncommitted |
Intro(1), getoptcvt(1), ksh(1), ksh93(1), ps(1), sh(1), getopt(3C), attributes(7), environ(7), standards(7)
Whenever an error is detected and the first character in the optstring operand is not a colon (:), a diagnostic message is written to standard error with the following information in an unspecified format:
basename "$0"
can be used.
November 2, 2007 | OmniOS |