up | Inhaltsverzeichniss | Kommentar

Manual page for GETOPTS(1)

getopts, getoptcvt - parse command options in shell scripts

SYNOPSIS

getopts optstring name [ argument... ]

getoptcvt [ -b ] filename

DESCRIPTION

getopts is used by shell procedures to parse positional parameters and to check for legal options. It should be used in place of the getopt.1v command. It supports the following command syntax rules:

optstring must contain the option letters the command using getopts will recognize; if a letter is followed by a colon, the option is expected to have an argument, or group of arguments, which must be separated from it by white space.

Each time it is invoked, getopts will place the next option in the shell variable name and the index of the next argument to be processed in the shell variable OPTIND. Whenever the shell or a shell procedure is invoked, OPTIND is initialized to 1.

When an option requires an option-argument, getopts places it in the shell variable OPTARG.

If an illegal option is encountered, ? will be placed in name.

When the end of options is encountered, getopts exits with a non-zero exit status. The special option `--' may be used to delimit the end of the options.

By default, getopts parses the positional parameters. If extra arguments (argument ...) are given on the getopts command line, getopts will parse them instead.

getoptcvt reads the shell script in filename, converts it to use getopts instead of getopt, and writes the results on the standard output.

OPTIONS

getoptcvt

-b
Generate a script that will be portable to earlier releases of the UNIX system. The script will determine at run time whether to invoke getopts or getopt.

EXAMPLE

The following fragment of a shell program shows how one might process the arguments for a command that can take the options a or b, as well as the option o, which requires an option-argument:


while getopts abo: c
do
	case $c in
	a | b)	FLAG=$c;;
	o)	OARG=$OPTARG;;
	\?)	echo $USAGE
		exit 2;;
	esac
done
shift `expr $OPTIND - 1`

This code will accept any of the following as equivalent:

cmd -a -b -o "xxx z yy" filename
cmd -a -b -o "xxx z yy" -- filename
cmd -ab -o xxx,z,yy filename
cmd -ab -o "xxx z yy" filename
cmd -o xxx,z,yy -b -a filename

SEE ALSO

getopt.1v sh.1 getopt.3

WARNING

Changing the value of the shell variable OPTIND or parsing different sets of arguments may lead to unexpected results.

DIAGNOSTICS

getopts prints an error message on the standard error when it encounters an option letter not included in optstring.


index | Inhaltsverzeichniss | Kommentar

Created by unroff & hp-tools. © somebody (See intro for details). All Rights Reserved. Last modified 11/5/97