12.4. Positional Parameters and Command-Line Arguments

 <  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

Variable

Function

$0

References the name of the script

$#

Holds the value of the number of positional parameters

$*

Contains a list of all the positional parameters

$@

Means the same as $* , except when enclosed in double quotes

"$*"

Expands to a single argument, for example, "$1 $2 $3"

"$@"

Expands to separate arguments, for example, "$1" "$2" "$3"


12.4.1 The set Command and Positional Parameters

The 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

  1. The name of the script is stored in the $0 variable.

  2. $* (and $@ ) both represent all of the positional parameters.

  3. $1 represents the first positional parameter (command-line argument).

  4. $2 represents the second positional parameter.

  5. $# is the total number of positional parameters (command-line arguments).

  6. The variable oldparameters is assigned all of the positional parameters ( $* ). Later on, if you want to get back your original parameters, you can do so by typing set $oldparameters .

  7. Reset positional parameters with the set command. The set command completely clears all previously set parameters. Jake is assigned to $1 , Nicky is assigned to $2 , and Scott is assigned to $3 .

  8. The new positional parameters are printed.

  9. The number of positional parameters is printed.

  10. The original parameters were stored in the variable oldparameters . They are printed.

  11. All parameters are unassigned .

  12. $1 has no value. The parameters list was cleared with the set “ “ command.

  13. A new parameter list is assigned by substituting the values in oldparameters to the parameter list with the set command.

  14. All the positional parameters are printed.

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

  1. The positional parameters are set. When the $* is expanded, the quotes are stripped and apple pie becomes two separate words. The for loop assigns each of the words, in turn , to the variable i and then prints the value of i . Each time through the loop, the word on the left is shifted off, and the next word is assigned to i .

  2. If $* is surrounded by double quotes, all of the words in the list become one single string, and the whole string is assigned to the variable i .

  3. The positional parameters are set.

  4. By enclosing $* in double quotes, the entire parameter list becomes one string.

  5. The positional parameters are set.

  6. Unquoted, the $@ behaves the same way as the $* .

  7. The positional parameters are set.

  8. By surrounding $@ with double quotes, each of the positional parameters is treated as a quoted string. The list would consist of "apple pie" , "pears" , and "peaches" . Each of the quoted words is assigned to i , in turn, as the loop goes through each iteration.

 <  Day Day Up  >  


UNIX Shells by Example
UNIX Shells by Example (4th Edition)
ISBN: 013147572X
EAN: 2147483647
Year: 2004
Pages: 454
Authors: Ellie Quigley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net