Previous | Table of Contents | Next |
DESCRIPTION
The internal getopts command is used in shell scripts to parse the positional parameters. It checks for legal options as specified in the command rules. Refer to Module 23 describing Commands for a list of the rules. The getopts command supports rules 3 through 10. It replaces the previous command parser getopt which did not support all of the command rules.
COMMAND FORMAT
Following is the general format of the getopts command.
getopts optstring name [ arg ... ] getopts optstring -- name [ arg ... ]
Arguments
The following arguments may be passed to the getopts command.
optstring | Must contain valid options your shell recognizes. If a character is followed by a colon (:), an option argument is expected. The argument is stored in the variable OPTARG. For example, if you plan on using options i, o that has an argument, and v, then you would have to write your getopts command as follows : | |
getopts vio: OPTION | ||
valid command lines would be: | ||
cmd -iv -o oarg file cmd -iv -o oarg -- file cmd -i -v -o oarg file cmd -v -o oarg -i file cmd -o oarg -i -v file | ||
Command line options must be preceded by a - or + sign. | ||
name | Each time getopts is invoked (called within the shell) the next option is placed in the shell variable name . For example, in the code | |
while getopts abc: OPTION | ||
... | ||
with a command line of | ||
cmd -b -a -c myfile | ||
the OPTION variable becomes b on the first iteration of the loop, a on the second, and c on the third. | ||
If a + is attached to the option on the command line, a + is attached to the beginning of the value of the name variable. | ||
arg | If arg uments are specified, getopts will parse them instead of the positional parameters passed to the shell script at invocation time. For instance, | |
getopts abc: OPTION a c afile | ||
causes a, c, and afile to be used as positional parameters 1, 2, and 3, respectively. | ||
-- | Delimits the end of all options and the beginning of all arguments if any exist. |
Command Line The command line must conform to the standard rules specified in the Command Rules section of Module 23.
Command line options must be preceded by a - or + sign.
The first command line argument that is not preceded by a - or + sign is considered the start of all arguments and the end of all options.
FURTHER DISCUSSION
The index (positional parameter number) of the next argument to be processed is placed in the shell variable OPTIND. For example, based on the following two commands:
cmd -a -b -c file cmd -ab -c file
OPTIND is set to 2 after the -a option is processed, because the next option to be processed (b) is in the second positional parameter. In the second command OPTIND is set to 1 after the -a option is processed, because the next option to be processed (b) is in first positional parameter($1). OPTIND is initialized to one when the shell is first invoked.
If an option requires an argument (a colon was placed after it in the optstring ), the argument is placed in the shell variable OPTARG.
Invalid Options and Optargs
Depending on the optstring layout getopts handles invalid options in different ways.
If optstring begins with a : and an invalid option is encountered , getopts puts the bad option in the shell variable OPTARG and sets name to ?. If optstring begins with a : and an option argument is omitted, getopts sets name to : and sets OPTARG to the options encountered.
If optstring does not begin with a : and an invalid option is encountered, getopts sets names to ? and displays an error message.
The getopts command is invoked for each iteration of the while loop. Thus OPTION is set to the value of the next option on the command line. The case uses the value in OPTION to determine what function is to be performed.
The following example illustrates how you can use the getopts command in a shell script.
while getopts :abc: OPTION do case $OPTION in a) A_FLG="yes" ;; b) B_FLG="on" ;; c) C_OPT=$OPTARG ;; :) echo "while getopts :abc: OPTION do case $OPTION in a) A_FLG="yes" ;; b) B_FLG="on" ;; c) C_OPT=$OPTARG ;; :) echo "$0: $OPTARG missing argument!" exit 2 ;; \?) echo "$0: $OPTARG is an invalid option!" echo "$0 -ab -c optarg file_list" exit 2 ;; esac done shift $OPTIND-1: $OPTARG missing argument!" exit 2 ;; \?) echo "while getopts :abc: OPTION do case $OPTION in a) A_FLG="yes" ;; b) B_FLG="on" ;; c) C_OPT=$OPTARG ;; :) echo "$0: $OPTARG missing argument!" exit 2 ;; \?) echo "$0: $OPTARG is an invalid option!" echo "$0 -ab -c optarg file_list" exit 2 ;; esac done shift $OPTIND-1: $OPTARG is an invalid option!" echo "while getopts :abc: OPTION do case $OPTION in a) A_FLG="yes" ;; b) B_FLG="on" ;; c) C_OPT=$OPTARG ;; :) echo "$0: $OPTARG missing argument!" exit 2 ;; \?) echo "$0: $OPTARG is an invalid option!" echo "$0 -ab -c optarg file_list" exit 2 ;; esac done shift $OPTIND-1-ab -c optarg file_list" exit 2 ;; esac done shift $OPTIND-1
DIAGNOSTICS AND BUGS
If getopts encounters an option not specified in the optstring , it displays an error message on the standard error.
RELATED FILES
The getopts command displays its output to the standard output.
Previous | Table of Contents | Next |