Previous Table of Contents Next


Module 59
getopts (ksh)

DESCRIPTION

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.

COMMAND FORMAT

Following is the general format of the getopts command.

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

Arguments

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.

FURTHER DISCUSSION

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

DIAGNOSTICS AND BUGS

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

RELATED FILES

The getopts command displays its output to the standard output.

APPLICATIONS

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.

TYPICAL OPERATION

In this activity you use the getopts command to parse the command line arguments and echo them back to your screen. Begin at the shell prompt.

1.  Type in the following lines of shell code using the vi editor. Call the file do_opts. Type vi do_opts to enter the editor.
       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
2.  When you have finished, save the file and change the modes to 755 by typing chmod 755 do_opts.
3.  Now test the new command by typing do_opts -a -c afile and pressing Return. Your output should resemble the following.
       cj> do_opts -a -c afile
       A_FLG=yes
       C_OPT=afile
4.  Type do_opts -b -x afile and press Return to cause an error in the options. Your screen should resemble the following display.
       cj> do_opts -b -x afile
       B_FLG=no
       do_opts: -x is an invalid option!
       do_opts -ab -c optarg file_list
5.  Type do_opts -b -a -c and press Return to cause an error in the option argument required for option c. Your screen should resemble the following display.
       cj> do_opts -b -a -c
       B_FLG=b
       A_FLG=a
       do_opts: -c option missing argument!
6.  Turn to Module 6 to continue the learning sequence.


Previous Table of Contents Next