Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ getopts(1) — SunOS 5.4

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

getopt(1)

getoptcvt(1)

intro(1)

ksh(1)

sh(1)

getopts(1)

NAME

getopts − shell built-in function to parse command-line options

SYNOPSIS

sh

getopts optstring name [ argument ...]

ksh

getopts optstring name [ arg ... ]

DESCRIPTION

sh

getopts is a built-in Bourne shell command used to parse positional parameters and to check for valid options.  See sh(1).  It supports all applicable rules of the command syntax standard (see Rules 3-10, intro(1)).  It should be used in place of the getopt command. 

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 places 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 script 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 parses them instead. 

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

So that all new commands will adhere to the command syntax standard described in intro(1), they should use getopts or getopt to parse positional parameters and check for options that are valid for that command. 

Examples:

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 accepts 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

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

Although the following command syntax rule (see intro(1)) relaxations are permitted under the current implementation, they should not be used because they may not be supported in future releases of the system.  As in the EXAMPLES section above, a and b are options, and the option o requires an option-argument.  The following example violates Rule 5:  options with option-arguments must not be grouped with other options:

example% cmd −aboxxx filename

The following example violates Rule 6: there must be white space after an option that takes an option-argument:

example% cmd −ab −oxxx filename

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

ksh

Checks arg for legal options.  If arg is omitted, the positional parameters are used.  An option argument begins with a + or a −.  An option not beginning with + or − or the argument −− ends the options.  optstring contains the letters that getopts recognizes.  If a letter is followed by a :, that option is expected to have an argument.  The options can be separated from the argument by blanks.  getopts places the next option letter it finds inside variable name each time it is invoked with a + prepended when arg begins with a +.  The index of the next arg is stored in OPTIND.  The option argument, if any, gets stored in OPTARG.  A leading : in optstring causes getopts to store the letter of an invalid option in OPTARG, and to set name to ?  for an unknown option and to : when a required option is missing.  Otherwise, getopts prints an error message.  The exit status is non-zero when there are no more options. 

For a further discussion of the Korn shell’s getopts built-in command, see the previous discussion in the Bourne shell, sh, section of this manpage. 

SEE ALSO

getopt(1), getoptcvt(1), intro(1), ksh(1), sh(1)

SunOS 5.4  —  Last change: 15 Apr 1994

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