UNIX Shells by Example (4th Edition)
| < Day Day Up > |
| Shell scripts can take command-line arguments. Arguments are used to modify the behavior of the program in some way. The C shell assigns command-line arguments to positional parameters and enforces no specific limit on the number of arguments that can be assigned (the Bourne shell sets a limit of nine positional parameters). Positional parameters are number variables . The script name is assigned to $0 , and any words following the scriptname are assigned to $1 , $2 , $3 . . . ${10} , ${11} , and so on. $1 is the first command-line argument. In addition to using positional parameters, the C shell provides the argv built-in array. 10.5.1 Positional Parameters and argv
If using the argv array notation, a valid subscript must be provided to correspond to the argument being passed in from the command line or the error message Subscript out of range is sent by the C shell. The argv array does not include the script name. The first argument is $argv[1] , and the number of arguments is represented by $#argv . (There is no other way to represent the number of arguments.) See Table 10.4. Example 10.10.
(The Script) #!/bin/csh f # The greetings script # This script greets a user whose name is typed in at the command line. 1 echo (The Script) #!/bin/csh “f # The greetings script # This script greets a user whose name is typed in at the command line. 1 echo $0 to you $1 $2 $3 2 echo Welcome to this day `date awk '{print $1, $2, $3}'` 3 echo Hope you have a nice day, $argv[1] \! 4 echo Good “bye $argv[1] $argv[2] $argv[3] (The Command Line) % chmod +x greetings % greetings Guy Quigley 1 greetings to you Guy Quigley 2 Welcome to this day Fri Aug 28 3 Hope you have a nice day, Guy! 4 Subscript out of range to you 2 echo Welcome to this day `date awk '{print , , }'` 3 echo Hope you have a nice day, $argv[1] \! 4 echo Goodbye $argv[1] $argv[2] $argv[3] (The Command Line) % chmod +x greetings % greetings Guy Quigley 1 greetings to you Guy Quigley 2 Welcome to this day Fri Aug 28 3 Hope you have a nice day, Guy! 4 Subscript out of range
Table 10.4. Command-Line Arguments
EXPLANATION
|
| < Day Day Up > |