Softpanorama
(slightly skeptical) Open Source Software Educational Society

May the source be with you, but remember the KISS principle ;-)

Google   


getopts (ksh)

The internal getopts command is used in shell scripts to parse the positional parameters. It checks for legal options as specified in the command rules. Refer to Module 23 describing Commands for a list of the rules. The getopts command supports rules 3 through 10. It replaces the previous command parser getopt which did not support all of the command rules. The getopts command provides a standard command parser for your shell scripts. If you learn to use it to parse the options for your shell scripts, you won't have to code for valid combinations of options. It checks for valid options then formats them into an easy-to-work-with string. If you are using the csh, skip to the next module.

Following is the general format of the getopts command.

     getopts optstring name [ arg ... ]
     getopts optstring -- name [ arg ... ]

The following arguments may be passed to the getopts command.

optstring Must contain valid options your shell recognizes. If a character is followed by a colon (:), an option argument is expected. The argument is stored in the variable OPTARG. For example, if you plan on using options i, o that has an argument, and v, then you would have to write your getopts command as follows:
    getopts vio: OPTION
  valid command lines would be:
    cmd -iv -o oarg file
cmd -iv -o oarg -- file
cmd -i -v -o oarg file
cmd -v -o oarg -i file
cmd -o oarg -i -v file
  Command line options must be preceded by a - or + sign.
name Each time getopts is invoked (called within the shell) the next option is placed in the shell variable name. For example, in the code
    while getopts abc: OPTION
    ...
  with a command line of
    cmd -b -a -c myfile
  the OPTION variable becomes b on the first iteration of the loop, a on the second, and c on the third.
  If a "+" is attached to the option on the command line, a "+" is attached to the beginning of the value of the name variable.
arg If arguments are specified, getopts will parse them instead of the positional parameters passed to the shell script at invocation time. For instance,
    getopts abc: OPTION a c afile
  causes a, c, and afile to be used as positional parameters 1, 2, and 3, respectively.
-- Delimits the end of all options and the beginning of all arguments if any exist.

Command Line    The command line must conform to the standard rules specified in the Command Rules section of Module 23.

Command line options must be preceded by a - or + sign.

The first command line argument that is not preceded by a - or + sign is considered the start of all arguments and the end of all options.

 

The index (positional parameter number) of the next argument to be processed is placed in the shell variable OPTIND. For example, based on the following two commands:

     cmd -a -b -c file
     cmd -ab -c file

OPTIND is set to 2 after the -a option is processed, because the next option to be processed (b) is in the second positional parameter. In the second command OPTIND is set to 1 after the -a option is processed, because the next option to be processed (b) is in first positional parameter($1). OPTIND is initialized to one when the shell is first invoked.

If an option requires an argument (a colon was placed after it in the optstring), the argument is placed in the shell variable OPTARG.

Invalid Options and Optargs

Depending on the optstring layout getopts handles invalid options in different ways.

If optstring begins with a : and an invalid option is encountered, getopts puts the bad option in the shell variable OPTARG and sets name to ?. If optstring begins with a : and an option argument is omitted, getopts sets name to : and sets OPTARG to the options encountered.

If optstring does not begin with a : and an invalid option is encountered, getopts sets names to ? and displays an error message.

The getopts command is invoked for each iteration of the while loop. Thus OPTION is set to the value of the next option on the command line. The case uses the value in OPTION to determine what function is to be performed.

The following example illustrates how you can use the getopts command in a shell script.

       while getopts :abc: OPTION
        do
         case $OPTION in
          a) A_FLG="yes" ;;
          b) B_FLG="on"  ;;
          c) C_OPT=$OPTARG ;;
          :) echo "$0: $OPTARG missing argument!"
             exit 2 ;;
         \?) echo "$0: $OPTARG is an invalid option!"
             echo "$0 -ab -c optarg file_list"
             exit 2 ;;
         esac
         done
       shift $OPTIND-1

 

If getopts encounters an option not specified in the optstring, it displays an error message on the standard error.

The getopts command displays its output to the standard output.

Examples

Type in the following lines of shell code using the vi editor. Call the file do_opts.
       while getopts :abc: OPTION
        do
         case "$OPTION" in
          a) A_FLG="yes"   ; echo "A_FLG=$A_FLG" ;;
          b) B_FLG="on"    ; echo "B_FLG=$B_FLG" ;;
          c) C_OPT=$OPTARG ; echo "C_OPT=$C_OPT ;;
          :) echo "$0: $OPTARG option missing argument!"
             exit 2 ;;
          ?) echo "$0: $OPTARG is an invalid option!"
             echo "$0 -ab -c optarg file_list"
             exit 2 ;;
         esac
         done
       shift $OPTIND-1
 do_opts -b -x afile
       B_FLG=no
       do_opts: -x is an invalid option!
       do_opts -ab -c optarg file_list
 do_opts -b -a -c
       B_FLG=b
       A_FLG=a
       do_opts: -c option missing argument!

Copyright © 1996-2008 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

Standard disclaimer: The statements, views and opinions presented on this web page are those of the author and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.

Last modified: February 28, 2008