10.3 Command Line Arguments

   

You can pass information to a shell program using command line arguments, the same as any other HP-UX command. These command line arguments can be used within the shell program. They are stored in variables , accessible within the program.

Using Command Line Arguments, or Positional Parameters

The command line arguments are stored in variables that show the position of the argument in the command line. That is why these are also called positional parameters. The variables that store command line arguments have names from $0 to $9 . Beginning with the tenth command line argument, the argument number is enclosed in braces. The variable names are shown in Table 10-1.

Table 10-1. Variables for Storing Command Line Arguments
Variable Name Description
$0 Shows value of the command itself (program name)
$1 First command line argument
$2 Second command line argument
. .
. .
. .
$9 Ninth command line argument
${10} Tenth command line argument
$# Total number of command line arguments
$* A space-separated list of command line arguments

Let's see script-05 , which shows how many command line arguments are provided, a list of all arguments, and the value of the first argument. This program is now shown using the cat command.

 $  cat script-05  #!/usr/bin/sh echo "Total number of command line arguments is: $#" echo "These arguments are: $*" echo "The first argument is: " $ 

When you execute the program with three arguments red , green , and blue , the result is as shown below.

 $  ./script-05 red green blue  Total number of command line arguments is: 3 These arguments are: red green blue The first argument is: red $ 

The shift Command

The shift command is used to move the command line arguments one position left. The first argument is lost when you use the shift command. Shifting command line arguments is useful when you perform a similar action to all arguments, one-by-one, without changing the variable name. The shift command throws away the left-most variable (argument number 1) and reassigns values to the remaining variables. The value in $2 moves to $1 , the value in $3 moves to $2 , and so on. Let's modify script-05 into script-06 as shown below using the cat command.

 $  cat script-06  #!/usr/bin/sh echo "Total number of command line arguments is: $#" echo "These arguments are: $*" echo "The first argument is: " shift echo "New first argument after shift: " shift echo "First argument after another shift: " $ 

Now let's execute script-06 with the same three arguments we used with script-05 . You can see from the next result that after every shift, a new value is assigned to $1 . This value is the variable that is just on the right side of $1 (i.e., $2 ).

 $  ./script-06 red green blue  Total number of command line arguments is: 3 These arguments are: red green blue The first argument is: red New first argument after shift: green First argument after another shift: blue $ 

During the first shift operation, $1 value is lost forever and can't be recovered by the program. The shift command can also do multiple shift operations in one step. For this you need to supply an argument to the shift command. For example, shift 2 will shift two arguments in one step, such that the old values of $1 and $2 will be lost, the value of $3 will be assigned to $1 , the value of $4 will be assigned to $2 , and so on.

Study Break

Use of Variables in Shell Programs

Variables play an important role in shell programs. To have some practice with the shell variables, modify script-06 so that before using the shift command, you store the value contained in $1 in another variable. Use the shift 2 command in the program and then try to print the $1 variable. Print the old value of the $1 variable using the echo command.


   
Top


HP Certified
HP Certified: HP-UX System Administration
ISBN: 0130183741
EAN: 2147483647
Year: 2000
Pages: 390
Authors: Rafeeq Rehman

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