getopts(1) getopts(1)
NAME
getopts, getoptcvt - parse command options
SYNOPSIS
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 valid options. It
supports all applicable rules of the command syntax standard
[see Rules 3-10, intro(1), and the NOTICES section]. It
should be used in place of the getopt(1) command. getopts
recognizes supplementary code set characters in the argument
given to optstring according to the locale specified in the
LC_CTYPE environment variable [see LANG on environ(5)].
USAGE
optstring must contain the option letters that 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 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 procedure is invoked, OPTIND is initialized to 1.
(OPTIND is not initialized to 1 when a shell function is
called.)
When an option requires an option-argument, getopts places it
in the shell variable OPTARG.
If an invalid 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 -- can be used to
delimit the end of the options.
By default, getopts parses the positional parameters. If
arguments arg . . . are given on the getopts command line,
getopts parses them instead.
Copyright 1994 Novell, Inc. Page 1
getopts(1) getopts(1)
/usr/lib/getoptcvt reads the shell script in file, converts it
to use getopts instead of getopt, and writes the results on
the standard output.
-b Make the converted script portable to earlier releases
of the UNIX system. /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 or getopt.
So all new commands will adhere to the command syntax standard
described in intro(1), they should use getopts or getopt(3C)
to parse positional parameters and check for options that are
valid for that command (see the NOTICES section).
Example
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 accepts any of the following as equivalent:
cmd -a -b -o "xxx z yy" file
cmd -a -b -o "xxx z yy" -- file
cmd -ab -o xxx,z,yy file
cmd -ab -o "xxx z yy" file
cmd -o xxx,z,yy -b -a file
Files
/usr/lib/locale/locale/LC_MESSAGES/uxcore.abi
language-specific message file [See LANG on environ (5).]
Copyright 1994 Novell, Inc. Page 2
getopts(1) getopts(1)
Output
getopts prints an error message on the standard error when it
encounters an option letter not included in optstring.
NOTICES
Although the following command syntax rule [see intro(1)]
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. The following example violates Rule 5:
options with option-arguments must not be grouped with other
options:
cmd -aboxxx file
The following example violates Rule 6: there must be white
space after an option that takes an option-argument:
cmd -ab -oxxx file
Changing the value of the shell variable OPTIND or parsing
different sets of arguments can lead to unexpected results.
REFERENCES
getopt(1), getopt(3C), intro(1), sh(1)
Copyright 1994 Novell, Inc. Page 3