GETOPTS(C) UNIX System V
Name
getopts, getoptcvt - parses command options
Syntax
getopts optstring name [arg ...]
/usr/lib/getoptcvt [-b] file
Description
The getopts command is used by shell procedures to parse
positional parameters and to check for legal options. It
supports all applicable rules of the command syntax standard
[see Rules 3-10, Intro(C)]. It should be used in place of
the getopt(C) command. (See the Notes below.)
This feature is only available in the Bourne shell.
optstring must contain the option letters the command using
getopts will recognize; 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 will place 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 procedure 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, ? will be placed in
name.
When the end of options is encountered, getopts exits with a
non-zero exit status. The special option ``--'' may be used
to delimit the end of the options.
By default, getopts parses the positional parameters. If
extra arguments (arg ...) are given on the getopts command
line, getopts will parse them instead.
The /usr/lib/getoptcvt command reads the shell script in
file, converts it to use getopts(C) instead of getopt(C),
and writes the results to the standard output.
-b the results of running /usr/lib/getoptcvt will be
portable to earlier UNIX releases. /usr/lib/getoptcvt
modifies the shell script in file so that when the
resulting shell script is executed, it determines at
run time whether to invoke getopts(C) or getopt(C).
So all new commands will adhere to the command syntax
standard described in Intro(C), they should use getopts(C)
or getopt(S) to parse positional parameters and check for
options that are legal for that command (see Notes below).
Examples
The following fragment of a shell program 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
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`
This code will accept any of the following as equivalent:
cmd -a -b -o "xxx z yy"
cmd -a -b -o "xxx z yy" --
cmd -ab -o xxx,z,yy
cmd -ab -o "xxx z yy"
cmd -o xxx,z,yy -b -a
See Also
Intro(C), sh(C), getopt(S)
Notes
Although the following command syntax rule [see Intro(C)]
relaxations are permitted under the current implementation,
they should not be used because they may not be supported in
future releases of the system. As in the Example section
above, a and b are options, and the option o requires an
option-argument:
cmd -aboxxx file (Rule 5 violation: options with
option-arguments must not be grouped with other options.)
cmd -ab -oxxx file (Rule 6 violation: there must be
white space after an option that takes an option-argument.)
Changing the value of the shell variable OPTIND or parsing
different sets of arguments may lead to unexpected results.
Diagnostics
getopts prints an error message to the standard error when
it encounters an option letter not included in optstring.
(printed 8/28/89) GETOPTS(C)