Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

getopt(1V)

sh(1)

getopt(3)

GETOPTS(1)  —  USER COMMANDS

NAME

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:

• Option names must be one character long. 

• All options must be preceded by ‘−’. 

• Options with no arguments may be grouped after a single ‘−’. 

• Option-arguments cannot be optional. 

• All options must precede operands on the command line. 

• ‘−−’ may be used to indicate the end of the options. 

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.

Solbourne Computer, Inc.  —  7 September 1989

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026