< Day Day Up > |
8.4. Positional Parameters and Command-Line ArgumentsInformation can be passed into a script via the command line. Each word (separated by whitespace) following the script name is called an argument. Command-line arguments can be referenced in scripts with positional parameters; for example, $1 for the first argument, $2 for the second argument, $3 for the third argument, and so on. The $# variable is used to test for the number of parameters, and $* is used to display all of them. Positional parameters can be set or reset with the set command. When the set command is used, any positional parameters previously set are cleared out. See Table 8.3. Example 8.7.(The Script) #!/bin/sh # Scriptname: greetings echo "This script is called $0." 1 echo "$0 $1 and $2" echo "The number of positional parameters is $#" ----------------------------------------------------------- (The Command Line) $ chmod +x greetings 2 $ greetings This script is called greetings. greetings and The number of positional parameters is 3 $ greetings Tommy This script is called greetings. greetings Tommy and The number of positional parameters is 1 4 $ greetings Tommy Kimberly This script is called greetings. greetings Tommy and Kimberly The number of positional parameters is 2
EXPLANATION
8.4.1 The set Command and Positional ParametersThe set command with arguments resets the positional parameters.[1] Once reset, the old parameter list is lost. To unset all of the positional parameters, use set – –. $0 is always the name of the script.
Example 8.8.(The Script) #!/bin/sh # Scriptname: args # Script to test command-line arguments 1 echo The name of this script is $0. 2 echo The arguments are $*. 3 echo The first argument is $1. 4 echo The second argument is $2. 5 echo The number of arguments is $#. 6 oldargs=$* # Save parameters passed in from the command line 7 set Jake Nicky Scott # Reset the positional parameters 8 echo All the positional parameters are $*. 9 echo The number of postional parameters is $#. 10 echo "Good–bye for now, $1 " 11 set `date` # Reset the positional parameters 12 echo The date is $2 $3, $6. 13 echo "The value of \$oldargs is $oldargs." 14 set $oldargs 15 echo $1 $2 $3 (The Command Line and Output) $ args a b c d 1 The name of this script is args. 2 The arguments are a b c d. 3 The first argument is a. 4 The second argument is b. 5 The number of arguments is 4. 8 All the positional parameters are Jake Nicky Scott. 9 The number of positional parameters is 3. 10 Good-bye for now, Jake 12 The date is Mar 25, 2004. 13 The value of $oldargs is a b c d. 15 a b c EXPLANATION
Example 8.9.(The Script) #!/bin/sh # Scriptname: checker # Script to demonstrate the use of special variable modifiers and arguments 1 name=${1:?"requires an argument" } echo Hello $name (The Command Line) 2 $ checker ./checker: 1: requires an argument 3 $ checker Sue Hello Sue EXPLANATION
8.4.2 How $* and $@ DifferThe $* and $@ differ only when enclosed in double quotes. When $* is enclosed within double quotes, the parameter list becomes a single string. When $@ is enclosed within double quotes, each of the parameters is quoted; that is, each word is treated as a separate string. Example 8.10.1 $ set 'apple pie' pears peaches 2 $ for i in $* > do > echo $i > done apple pie pears peaches 3 $ set 'apple pie' pears peaches 4 $ for i in "$*" > do > echo $i > done apple pie pears peaches 5 $ set 'apple pie' pears peaches 6 $ for i in $@ > do > echo $i > done apple pie pears peaches 7 $ set 'apple pie' pears peaches 8 $ for i in "$@" # At last!! > do > echo $i > done apple pie pears peaches EXPLANATION
|
< Day Day Up > |