< Day Day Up > |
Command-line arguments can be referenced in scripts with positional parameters; for example, $1 is set to the first argument, $2 to the second argument, and $3 to the third argument. Positional parameters can be reset with the set command. See Table 12.5. Table 12.5. Positional Parameters
12.4.1 The set Command and Positional ParametersThe set command sets the positional parameters. If the positional parameters have already been set, the set command will reset them, removing any values in the old list. To unset all of the positional parameters, use set “ “ . Example 12.12.(The Script) $ cat args #!/bin/ksh # Script to test command-line arguments 1 print The name of this script is(The Script) $ cat args #!/bin/ksh # Script to test command-line arguments 1 print The name of this script is $0. 2 print The arguments are $*. 3 print The first argument is $1. 4 print The second argument is $2 . 5 print The number of arguments is $#. 6 oldparameters=$* 7 set Jake Nicky Scott 8 print All the positional parameters are $*. 9 print The number of positional parameters is $#. 10 print $oldparameters 11 set -- 12 print Good “bye for now, $1 . 13 set $oldparameters 14 print $* (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 a b c d 12 Good-bye for now , 14 a b c d $. 2 print The arguments are $*. 3 print The first argument is . 4 print The second argument is . 5 print The number of arguments is $#. 6 oldparameters=$* 7 set Jake Nicky Scott 8 print All the positional parameters are $*. 9 print The number of positional parameters is $#. 10 print $oldparameters 11 set -- 12 print Goodbye for now, . 13 set $oldparameters 14 print $* (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 a b c d 12 Good-bye for now , 14 a b c d $ EXPLANATION
Example 12.13.(How $* and $@ Differ) 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 > |