Switch to SpeakEasy.net DSL

The Modular Manual Browser

Home Page
Manual: (SunOS-4.1.3)
Apropos / Subsearch:
optional field

GETOPTS(1)                  General Commands Manual                 GETOPTS(1)

       getopts, getoptcvt - parse command options in shell scripts

       getopts optstring name [ argument...  ]

       getoptcvt [ -b ] filename

       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:

              o    Option names must be one character long.

              o    All options must be preceded by `-'.

              o    Options  with  no  arguments  may be grouped after a single

              o    Option-arguments cannot be optional.

              o    All options must precede operands on the command line.

              o    `--' 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 sep-
       arated 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 argu-
       ments (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  out-

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

       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
                   case $c in
                   a | b)    FLAG=$c;;
                   o)        OARG=$OPTARG;;
                   \?)       echo $USAGE
                             exit 2;;
              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

       getopt(1V), sh(1), getopt(3)

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

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

                               7 September 1989                     GETOPTS(1)