< Day Day Up > |
14.10. The Command Line14.10.1 Processing Command-Line Options with getoptsIf you are writing scripts that require a number of command-line options, positional parameters are not always the most efficient. For example, the UNIX ls command takes a number of command-line options and arguments. (An option requires a leading dash; an argument does not.) Options can be passed to the program in several ways: ls laFi, ls i a l F , ls ia F , and so forth. If you have a script that requires arguments, positional parameters might be used to process the arguments individually, such as ls l i F . Each dash option would be stored in $1, $2, and $3, respectively. But, what if the user listed all of the options as one dash option, as in ls liF? Now the liF would all be assigned to $1 in the script. The getopts function makes it possible to process command-line options and arguments in the same way they are processed by the ls program.[8] The getopts function will allow the runit program to process its arguments using any variety of combinations.
Example 14.70.(The Command Line ) 1 $ runit x n 200 filex 2 $ runit xn200 filex 3 $ runit xy 4 $ runit yx n 30 5 $ runit n250 xy filey ( any other combination of these arguments ) EXPLANATION
Before getting into all the details of the runit program, we examine the line from the program where getopts is used to see how it processes the arguments. Example 14.71.
(A line from the script called runit)
while getopts :xyn: name
EXPLANATION x, y, and n are the options. In this example the first option is preceded by a colon. This tells getopts to use silent error reporting. If there is a colon after one of the options, the option expects an argument separated from it by whitespace. An argument is a word that does not begin with a dash. n requires an argument. Any options typed at the command line must begin with a dash. Any options that do not contain a dash tell getopts that the option list has ended. Each time getopts is called, it places the next option value it finds in the variable name. (You can use any variable name here.) If an illegal argument is given, name is assigned a question mark. Sample getopts ScriptsThe following examples illustrate how getopts processes arguments. Example 14.72.(The Script) #!/bin/bash # Program: opts1 # Using getopts First try 1 while getopts xy options do 2 case $options in 3 x) echo "you entered x as an option";; y) echo "you entered y as an option";; esac done (The Command Line) 4 $ opts1 x you entered x as an option 5 $ opts1 xy you entered x as an option you entered y as an option 6 $ opts1 y you entered y as an option 7 $ opts1 b opts1: illegal option -- b 8 $ opts1 b EXPLANATION
Example 14.73.(The Script) #!/bin/bash # Program: opts2 # Using getopts Second try 1 while getopts xy options 2> /dev/null do 2 case $options in x) echo "you entered x as an option";; y) echo "you entered y as an option";; 3 \?) echo "Only -x and -y are valid options" 1>&2;; esac done (The Command Line) $ opts2 x you entered x as an option $ opts2 y you entered y as an option $ opts2 xy $ opts2 xy you entered x as an option you entered y as an option 4 $ opts2 g Only -x and -y are valid options 5 $ opts2 c Only -x and -y are valid options EXPLANATION
Special getopts VariablesThe getopts function provides two variables to help keep track of arguments: OPTIND and OPTARG. OPTIND is a special variable that is initialized to one and is incremented each time getopts completes processing a command-line argument to the number of the next argument getopts will process. The OPTARG variable contains the value of a legal argument. See Examples 14.74 and 14.75. Example 14.74.(The Script) #!/bin/bash # Program: opts3 # Using getopts Third try 1 while getopts dq: options do case $options in 2 d) echo "d is a valid switch ";; 3 q) echo "The argument for -q is $OPTARG";; \?) echo "Usage:opts3 -dq filename ... " 1>&2;; esac done (The Command Line) 4 $ opts3 d d is a valid switch 5 $ opts3 -q foo The argument for -q is foo 6 $ opts3 -q Usage:opts3 -dq filename ... 7 $ opts3 e Usage:opts3 -dq filename ... 8 $ opts3 e EXPLANATION
Example 14.75.(The Script) #!/bin/bash # Program: opts4 # Using getopts Fourth try 1 while getopts xyz: arguments 2>/dev/null do case $arguments in 2 x) echo "you entered -x as an option .";; y) echo "you entered -y as an option." ;; 3 z) echo "you entered -z as an option." echo "\$OPTARG is $OPTARG.";; 4 \?) echo "Usage opts4 [-xy] [-z argument]" exit 1;; esac done 5 echo " The number of arguments passed was $(( $OPTIND - 1 ))" (The Command Line) $ opts4 -xyz foo You entered -x as an option. You entered -y as an option. You entered -z as an option. $OPTARG is foo. The number of arguments passed was 2. $ opts4 -x -y -z boo You entered -x as an option. You entered -y as an option. You entered -z as an option. $OPTARG is boo. The number of arguments passed was 4. $ opts4 -d Usage: opts4 [-xy] [-z argument] EXPLANATION
14.10.2 The eval Command and Parsing the Command LineThe eval command evaluates a command line, performs all shell substitutions, and then executes the command line. It is used when normal parsing of the command line is not flexible enough for what we want to do. Example 14.76.1 $ set a b c d 2 $ echo The last argument is \$$# 3 The last argument is $4 4 $ eval echo The last argument is \$$# The last argument is d 5 $ set -x $ eval echo The last argument is \$$# + eval echo the last argument is '$4' ++ echo the last argument is d The last argument is d EXPLANATION
Example 14.77.
(From Shutdown Program)
1 eval `/usr/bin/id | /usr/bin/sed 's/[^a-z0-9=].*//'`
2 if [ "${uid:=0}" -ne 0 ]
then
3 echo $0: Only root can run $0
exit 2
fi
EXPLANATION
|
< Day Day Up > |