
getopts(1) | getopts(1) |
getopts by default parses positional parameters passed to the invoking shell procedures. If args are given, they are parsed instead of the positional parameters.
EXTERNAL INFLUENCES
Environment Variable
The following environment variable affects the execution of the getopts utility:
OPTIND Used by getopts as the index of the next argument to be processed.
ERRORS
Whenever an error is detected and the ®rst character in the optstring operand is not a colon (:), a diagnostic message will be written to standard error with the following information in an unspeci®ed format:
∙The invoking program name will be identi®ed in the message. The invoking program name will be the value of the shell special parameter 0 at the time the getopts utility is invoked. A name equivalent to:
basename "$0"
may be used.
∙If an option is found that was not speci®ed in optstring, this error will be identi®ed and the invalid option character will be identi®ed in the message.
∙If an option requiring an
EXAMPLES
Since getopts affects the current shell execution environment, it is generally provided as a shell regular
(getopts abc value "$@") nohup getopts ...
find
it does not affect the shell variables in the caller's environment.
Note that shell functions share OPTIND with the calling shell even though the positional parameters are changed. Functions that use getopts to parse their arguments should save the value of OPTIND on entry and restore it before returning. However, there will be cases when a function must change OPTIND for the calling shell.
The following example script parses and displays its arguments:
aflag=
bflag=
while getopts ab: name do
case $name in a)
aflag=1;;
b)
bflag=1
bval="$OPTARG";;
?)
printf "Usage: %s:
esac
done
if [ !
printf "Option
fi
if [ !
printf "Option
fi
shift $(($OPTIND
printf "Remaining arguments are: %s\n" "$*"
g
− 2 − | Section 1−333 |