getopts(1) MPE/iX Shell and Utilities getopts(1)
______________________________________________________________________
NAME
getopts -- parse options from shell script command line
SYNOPSIS
getopts optstring name [arg ...]
DESCRIPTION
getopts obtains options and their arguments from a list of param-
eters that follows the standard POSIX.2 option syntax (that is,
single letters preceded by a - and possibly followed by an argu-
ment value). Typically, shell scripts use getopts to parse argu-
ments passed to them. When you specify args on the getopts com-
mand line, getopts parses those arguments instead of the script
command line (see set(1)).
The optstring gives all the option letters that the script recog-
nizes. For example, if the script recognizes -a, -f, and -s,
optstring is afs. If you want an option letter to be followed by
an argument value or group of values, put a colon after the
letter, as in a:fs. This indicates that getopts expects the -a
option to have the form
-a value
Normally one or more blanks separate the value from the option
letter; however, getopts also handles values that follow the
letter immediately, as in
-avalue
optstring can not contain the question mark (?) character.
The name on the getopts command line is the name of a shell vari-
able. Each time you invoke getopts, it obtains the next option
from the positional parameters and places the option letter in
the shell variable name.
getopts places a question mark (?) in name if it finds an option
that does not appear in optstring, or if an option value is miss-
ing.
Each option on the script command line has a numeric index. The
first option found has an index of 1, the second has an index of
2, and so on. When getopts obtains an option from the script
command line, it stores the index of the script in the shell
variable OPTIND.
When an option letter has an associated argument (indicated with
a : in optstring), getopts stores the argument as a string in the
shell variable OPTARG. If an option doesn't take an argument or
getopts expects an argument but doesn't find one, getopts unsets
OPTARG.
1
getopts(1) MPE/iX Shell and Utilities getopts(1)
______________________________________________________________________
When getopts reaches the end of the options, it exits with a sta-
tus value of 1. It also sets name to the character ? and sets
OPTIND to the index of the first argument after the options.
getopts recognizes the end of the options by any of the following
conditions:
* an argument that doesn't start with -
* the special argument --, marking the end of options
* an error (for example, an unrecognized option letter)
OPTIND and OPTARG are local to the shell script. If you want to
export them, you must do so explicitly. If the script invoking
getopts sets OPTIND to 1, it can call getopts again with a new
set of parameters, either the current positional parameters or
new arg values.
By default, getopts issues an error message if it finds an
unrecognized option or some other error. If you do not want such
messages printed, specify a colon as the first character in
opstring.
EXAMPLE
This is an example of using getopts in a shell script. Compare
it to the getopt example.
# Example illustrating use of getopts builtin. This
# shell script would implement the paste command,
# using getopts to process options, if the underlying
# functionality was embedded in hypothetical utilities
# hpaste and vpaste, which perform horizontal and
# vertical pasting respectively.
#
paste=vpaste # default is vertical pasting
seplist=" " # default separator is tab
while getopts d:s o
do case "$o" in
d) seplist="$OPTARG";;
s) paste=hpaste;;
[?]) print >&2 "Usage: $0 [-s] [-d seplist] file ..."
exit 1;;
esac
done
shift $OPTIND-1
# perform actual paste command
$paste -d "$seplist" "$@"
2
getopts(1) MPE/iX Shell and Utilities getopts(1)
______________________________________________________________________
ENVIRONMENT VARIABLES
getopts uses the following environment variables:
OPTARG
stores the value of the option argument found by getopts.
OPTIND
contains the index of the next argument to be processed.
DIAGNOSTICS
Possible exit status values are:
0 getopts found a script command line with the form of an
option. This happens whether or not it recognizes the option.
1 getopts reached the end of the options, or an error occurred.
Message
Because this utility is built into the MPE/iX Shell, see the
sh(1) man page for a complete list of error messages that you may
receive when using it.
PORTABILITY
POSIX.2. x/OPEN Portability Guide 4.0.
On UNIX systems, getopts is built into both the KornShell and
Bourne Shell.
NOTE
This command is built into the shell.
MPE/iX NOTES
The current release of MPE/iX uses the INFO string to pass argu-
ments to programs. If the size of this string plus the size of
the current environment (determined by the number and size of the
environment variables in the current process) is greater than
8192 bytes, the string is too long to pass to a subprocess and
the process creation fails.
SEE ALSO
sh(1), getopt(1)
3