getoptsparse options from shell script command line |
KornShell Built-in |
getopts
optstring name [arg ...]
getopts
obtains options and their arguments from a list of
parameters that follows the standard POSIX.2 option syntax (that is, single
letters preceded by a -
and possibly followed by an argument
value; the single letters may be grouped). Typically, shell scripts use
getopts
to parse arguments passed to them. When you specify
args on the getopts
command line,
getopts
parses those arguments instead of the script command
line (see set
).
The optstring gives all the option letters that the script recognizes.
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
Normally one or more blanks separate the value from the option letter; however,-a value
getopts
also handles values
that follow the letter immediately, as in
optstring cannot contain the question mark (-avalue
?
) character.
The name on the getopts
command line is the name of a
shell variable. 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 missing.
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 next argument to be processed 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
.
When getopts
reaches the end of the options it exits with
a status 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:
-
--
, marking the end of optionsOPTIND
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 optstring.
In addition to the standard POSIX.2 option syntax, getopts
can also recognize option letters which are preceded by a +
.
There are two ways of handling such options.
If optstring begins with :+
or +
, any options
beginning with +
set name to a string consisting of
+
followed by the option letter. The OPTARG
variable
is set as usual for options that take an argument.
If optstring begins with :++
or ++
or
+
appears anywhere in optstring other than the start, any
options beginning with +
set name to +
and the
OPTARG
variable to the option letter. With this method, it is not
possible to have options beginning with +
that take arguments.
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="\t" # 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" "$@"
OPTARG
stores the value of the option argument found by
getopts
.
OPTIND
contains the index of the next argument to be processed.
0
getopts
found a script command line argument
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.
2
Failure because of an invalid command line option.
getopts
is built into both the KornShell
and Bourne Shell.