Objet
Traite les arguments passés à un script et valide les options associées.
Lorsqu'une option attend un argument derrière, il faut placer un ":" derrière.
Exemple: "xc:d:" signifie que l'option 'x' n'attend pas d'argument, contrairement aux options c et d.
Dans ce cas là, la seconde option placée éventuellement derrière peut être absorbée. Ainsi, "-c -d" ne récupérera que l'option '-c', le '-d' étant absorbé dans l'attente de son argument.
Exemple :
export OPT=""
cpt=0
while getopts "xn" opt; do
case "$opt" in
h)
Usage
exit 0
;;
x) OPT="$OPT -x"
;;
n) OPT="$OPT -n"
;;
*) echo "Option inconnue : $opt"
exit 1
;;
esac
cpt=`expr $cpt + 1`
done
shift $cpt
A noter que le 'shift' en fin de boucle permet de retirer les options de la liste des arguments pour le script lui-même. Bien calculer les décalages à demander, suivant qu'il y a un argument ou pas.
Syntaxe officielle IBM.
getopts OptionString Name [ Argument ...]
Description
The getopts command is a Korn/POSIX Shell built-in command that retrieves options and option-arguments from a list of parameters. An option begins with a + (plus sign) or a - (minus sign) followed by a character. An option that does not begin with either a + or a - ends the OptionString. Each time the getopts command is invoked, it places the value of the next option in Name and the index of the next argument to be processed in the shell variable OPTIND. Whenever the shell is invoked, OPTIND is initialized to 1. When an option begins with +, a + is prepended to the value in Name.
If a character in OptionString is followed by a : (colon), that option is expected to have an argument. When an option requires an option-argument, the getoptscommand places it in the variable OPTARG.
When an option character not contained in OptionString is found, or an option found does not have the required option-argument:
- If OptionString does not begin with a : (colon),
- Name will be set to a ? (question mark) character,
- OPTARG. will be unset, and
- a diagnostic message will be written to standard error.
This condition is considered to be an error detected in the way arguments were presented to the invoking application, but is not an error in the processing of thegetopts command; a diagnostic message will be written as stated, but the exit status will be zero.
- If OptionString begins with a : (colon),
- Name will be set to a ? (question mark) character for an unknown option or to a : (colon) character for a missing required option,
- OPTARG will be set to the option character found, and
- no output will be written to standard error.
Any of the following identifies the end of options: the special option - -, finding an argument that does not begin with a -, or +, or encountering an error.
When the end of options is encountered:
- the getopts command will exit with a return value greater than zero,
- OPTARG will be set to the index of the first non-option-argument, where the first - - argument is considered to be an option-argument if there are no other non-option-arguments appearing before it, or the value $#+1 if there are no non-option-arguments,
- Name will be set to a ? (question mark) character.
Parameters
OptionString | Contains the string of option characters recognized by the getopts command. If a character is followed by a colon, that option is expected to have an argument, which should be supplied as a separate argument. The options can be separated from the argument by blanks. The first character in OptionString determines how the getopts command behaves if an option character is not known or an option-argument is missing.
Note: The characters question mark and colon must not be used as option characters by an application. The use of other characters that are not alphanumeric produces unspecified results. |
Name | Set by the getopts command to the option character that was found. |
Argument ... | One or more strings separated by white space, checked by the getopts command for legal options. If Argument is omitted, the positional parameters are used. See Parameter substitution in the Korn shell or POSIX shell in the Korn Shell for more information on positional parameters.
Note: Generally, you won't specify Argument as part of the getopts command, but it may be helpful when debugging your script. |
Exit Status
This command returns the following exit values:
0 | An option, specified or unspecified by OptionString, was found. |
>0 | The end of options was encountered or an error occurred. |
Examples
- The following getopts command specifies that a, b, and c are valid options, and that options a and c have arguments:
getopts a:bc: OPT
- The following getopts command specifies that a, b, and c are valid options, that options a and b have arguments, and that getopts set the value of OPT to ?when it encounters an undefined option on the command line:
getopts :a:b:c OPT
- The following script parses and displays it arguments:
aflag= bflag= while getopts ab: name do case $name in a) aflag=1;; b) bflag=1 bval="$OPTARG";; ?) printf "Usage: %s: [-a] [-b value] args\n" $0 exit 2;; esac done if [ ! -z "$aflag" ]; then printf "Option -a specified\n" fi if [ ! -z "$bflag" ]; then printf 'Option -b "%s" specified\n' "$bval" fi shift $(($OPTIND -1)) printf "Remaining arguments are: %s\n" "$*"