< Day Day Up > |
14.4. Positional Parameters and Command-Line Arguments14.4.1 Positional ParametersInformation 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. After $9, curly braces are used to keep the number as one number. For example, positional parameter 10 is referenced as ${10}. 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 14.2. Example 14.11.(The Script) #!/bin/bash # Scriptname: greetings2 echo "This script is called $0." 1 echo "$0 $1 and $2" echo "The number of positional parameters is $#" ----------------------------------------------------------- (The Command Line) $ chmod +x greetings2 2 $ greetings2 This script is called greetings2. greetings and The number of positional parameters is 3 $ greetings2 Tommy This script is called greetings2. greetings Tommy and The number of positional parameters is 1 4 $ greetings2 Tommy Kimberly This script is called greetings2. greetings Tommy and Kimberly The number of positional parameters is 2
EXPLANATION
14.4.2 The set Command and Positional ParametersThe set command with arguments resets the positional parameters.[3] 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 14.12.(The Script) #!/bin/bash # 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=$* 7 set Jake Nicky Scott # Reset the positional parameters 8 echo All the positional parameters are $*. 9 echo The number of positional 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 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 Wed Mar 25 EXPLANATION
Example 14.13.(The Script) #!/bin/bash # 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
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 14.14.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 > |