Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ getopts(1) — Reliant UNIX 5.44c4

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

getopt(1)

getoptcvt(1)

ksh(1)

sh(1)

getopts(1)                                                       getopts(1)

NAME
     getopts - scan command-line arguments for options (get options)

SYNOPSIS
     getopts optstring name [arg ...]

DESCRIPTION
     The shell built-in getopts is used by shell scripts to parse arguments
     and options in the command line and to check for valid options. It
     supports all applicable rules of the command syntax standard.

     getopts replaces the earlier getopt(1) command, which will not be sup-
     ported in future releases of the operating system. It is therefore
     recommended that you always use getopts in place of getopt from now
     on. Existing shell scripts can be converted to use getopts calls
     instead of getopt with the conversion utility getoptcvt(1).

     getopts can be used in shell scripts to parse the arguments supplied
     when the script is invoked. The individual options and arguments are
     placed sequentially in shell variables and can thus be easily read and
     checked. If an illegal option is encountered in the argument list, or
     if getopt does not find a corresponding argument for an option that
     requires one, an appropriate error message is printed.

     In order to ensure that the argument list is uniformly processed by
     all scripts and commands, getopts should always be used to parse the
     argument list and check for valid options.

OPERANDS
     optstring
          A string that may comprise letters and colons. getopts treats the
          letters specified in optstring as legal options of the shell
          script. If a letter is followed by a colon, getopts expects the
          option to take an argument or group of arguments, which must be
          separated from it by white space (blanks and tabs).

     name Name of the shell variable in which getopts places the next
          option each time it is invoked.

     arg ...
          Argument list parsed by getopts.

          arg not specified:

          getopts parses the argument list of the command line with which
          the script was invoked.

MODE OF OPERATION
     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 command interpreter or a
     shell script is invoked, OPTIND is initialized to 1.



Page 1                       Reliant UNIX 5.44                Printed 11/98

getopts(1)                                                       getopts(1)

     When an option requires an argument, getopts places the argument in
     the shell variable OPTARG. Options that require an argument must be
     identified by a colon in optstring.

     If an illegal option is encountered, a ? (question mark) is placed in
     the shell variable name. Illegal options are those which do not appear
     in optstring.

     When the end of the option list is encountered, getopts exits with a
     non-zero exit status. The special option -- may be also used to iden-
     tify the end of the options.

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

LOCALE
     The LCMESSAGES environment variable governs the language in which
     message texts are displayed. If LCMESSAGES is undefined or is defined
     as the null string, it defaults to the value of LANG. If LANG is like-
     wise undefined or null, the system acts as if it were not internation-
     alized.

































Page 2                       Reliant UNIX 5.44                Printed 11/98

getopts(1)                                                       getopts(1)

EXAMPLES
     The following fragment of a shell script scrpt shows how you might
     process the arguments for a script that can take the options -a or -b,
     as well as the option -o, which requires an option argument. Options
     -a and -b are mutually exclusive, and if both are specified, only the
     one listed second applies:

     while getopts abo: c
     do
           case $c in
           a | b)          FLAG=$c;;
           o)              OARG=$OPTARG;;
           \?)             echo "usage: $0 [-a | -b] [-o <arg>]"
                           exit 2;;
           esac
     done
     shift `expr $OPTIND - 1`

     This code accepts any of the following invocations of scrpt as
     equivalent:

     scrpt -a -b -o "xxx z yy" file
     scrpt -a -b -o "xxx z yy" -- file
     scrpt -ab -o xxx,z,yy file
     scrpt -ab -o "xxx z yy" file
     scrpt -o xxx,z,yy -b -a file

NOTES
     getopts exists both as an external command (/usr/bin/getopts) and as a
     built-in shell command, both in the Korn shell ksh(1) as well as in
     the Bourne shell sh(1). The shell generates a new process to execute
     /usr/bin/getopts.

     Some differences in behavior may occur when using getopts, depending
     on which shell is being used. The possible differences are not
     described specifically.

SEE ALSO
     getopt(1), getoptcvt(1), ksh(1), sh(1).















Page 3                       Reliant UNIX 5.44                Printed 11/98

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