getopts(1)getopts(1)NAME
getopts, getoptcvt - parse command options
SYNOPSIS
getopts optstring name [arg . . .]
/usr/lib/getoptcvt [-b] file
OPTIONS
Makes the converted script portable to earlier releases of the [Tru64
UNIX] system. The command getoptcvt modifies the shell script in a
file so that when the resulting shell script is executed, a choice is
made, during runtime, over invoking either the getopts or the getopt
command .
DESCRIPTION
The command getopts 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). It
should be used in place of the getopt command. (See the NOTES section
below.)
The string optstring must contain the option letters that the command
using getopts command will recognize; if a letter is followed by a
colon, the option is expected to have an argument, or group of argu‐
ments, which must be separated from it with white space.
Each time it is invoked, the getopts command places the next option in
the shell variable name and the index of the next argument to be pro‐
cessed in the shell variable OPTIND. Whenever the shell or a shell pro‐
cedure is invoked, OPTIND is initialized to 1.
When an option requires an option or argument, the getopts command
places it in the shell variable OPTARG.
If an illegal option is encountered, ? will be placed in the name.
When the end of options is encountered, the getopts command exits with
a non zero exit status. The special option “--” may be used to delimit
the end of the options.
By default, the getopts command parses the positional parameters. If
extra arguments (arg ...) are given on the getopts command line, the
getopts command parses them instead.
The command getoptcvt reads the shell script in file, converts it for
its usage with the getopts command instead of the getoptcommand and
writes the results on the standard output.
So all new commands will adhere to the command syntax standard
described in intro(1), they should use getopts or getopt to parse posi‐
tional parameters and check for options that are valid for that com‐
mand. (See the NOTES section below.)
NOTES
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 operating
system. As in the EXAMPLES section below, -a and -b are options, and
the option -o requires an option argument. The following example vio‐
lates 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 or argument: $ cmd -ab -oxxx file
Changing the value of the shell variable OPTIND or parsing different
sets of arguments may lead to unexpected results.
Some of the files that are referenced by absolute path in the System V
Environment for [Tru64 UNIX] man pages may actually be located rela‐
tive to /usr/opt/svr4. This is particularly true in the case where the
inclusive view option has not been installed.
EXIT STATUS
Success. An error occurred.
ERRORS
The command getopts prints an error message on the standard error when
it encounters an option letter not included in optstring.
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 or 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
SEE ALSO
Commands: intro(1), sh(1)
Functions: getopt(3C)getopts(1)