Previous Section  < Day Day Up >  Next Section

8.4. Positional Parameters and Command-Line Arguments

Information 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. 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 8.3.

Example 8.7.

(The Script)

     #!/bin/sh

     # Scriptname: greetings

     echo "This script is called $0."

1    echo "$0  $1 and $2"

     echo "The number of positional parameters is $#"

     -----------------------------------------------------------

(The Command Line)

     $ chmod +x greetings

2    $ greetings

     This script is called  greetings.

     greetings  and

     The number of positional parameters is



3    $ greetings Tommy

     This script is called greetings.

     greetings Tommy and

     The number of positional parameters is 1



4    $ greetings Tommy  Kimberly

     This script is called greetings.

     greetings Tommy and Kimberly

     The number of positional parameters is 2


Table 8.3. Bourne Shell Positional Parameters

Positional Parameter

What It References

$0

References the name of the script

$#

Holds the value of the number of positional parameters

$*

Lists all of the positional parameters

$@

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

"$*"

Expands to a single argument (e.g., "$1 $2 $3")

"$@"

Expands to separate arguments (e.g., "$1" "$2" "$3")

$1 .. $9

References up to nine positional parameters


EXPLANATION

  1. In the script greetings, positional parameter $0 references the script name, $1 the first command-line argument, and $2 the second command-line argument.

  2. The greetings script is executed without any arguments passed. The output illustrates that the script is called greetings ($0 in the script) and that $1 and $2 were never assigned anything; therefore, their values are null and nothing is printed.

  3. This time, one argument is passed, Tommy. Tommy is assigned to positional parameter 1.

  4. Two arguments are entered, Tommy and Kimberly. Tommy is assigned to $1 and Kimberly is assigned to $2.

8.4.1 The set Command and Positional Parameters

The set command with arguments resets the positional parameters.[1] 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.

[1] Remember, without arguments, the set command displays all the variables that have been set for this shell, local and exported. With options, the set command turns on and off shell control options such as –x and –v.

Example 8.8.

(The Script)

     #!/bin/sh

     # 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=$*             # Save parameters passed in from the command line

7    set Jake Nicky Scott   # Reset the positional parameters

8    echo All the positional parameters are $*.

9    echo The number of postional 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 Command Line and 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   a b c


EXPLANATION

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

  2. $* represents 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. All positional parameters are saved in a variable called oldargs.

  7. The set command allows you to reset the positional parameters, clearing out the old list. Now, $1 is Jake, $2 is Nicky, and $3 is Scott.

  8. $* represents all of the parameters, Jake, Nicky, and Scott.

  9. $# represents the number of parameters, 3.

  10. $1 is Jake.

  11. After command substitution is performed—that is, date is executed—the positional parameters are reset to the output of the date command.

  12. The new values of $2, $3, and $6 are displayed.

  13. The values saved in oldargs are printed.

  14. The set command creates positional parameters from the value stored in oldargs.

  15. The first three positional parameters are displayed.

Example 8.9.

(The Script)

     #!/bin/sh

     # 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

  1. The special variable modifier :? will check whether $1 has a value. If not, the script exits and the message is printed.

  2. The program is executed without an argument. $1 is not assigned a value; an error is displayed.

  3. The checker program is given a command-line argument, Sue. In the script, $1 is assigned Sue. The program continues.

8.4.2 How $* and $@ Differ

The $* 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 8.10.

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.

  2. When $* is expanded, the quotes surrounding apple pie are stripped; apple and pie become 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 the variable i.

  3. The positional parameters are set.

  4. By enclosing $* in double quotes, the entire parameter list becomes one string, "apple pie pears peaches". The entire list is assigned to i as a single word. The loop makes one iteration.

  5. The positional parameters are set.

  6. Unquoted, $@ and $* behave the same way (see line 2 of this explanation).

  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 be "apple pie", "pears", "peaches". The desired result is finally achieved.

    Previous Section  < Day Day Up >  Next Section