Using Arguments


You can pass arguments to scripts just as with any other command. The arguments to a script are accessed inside the script using a series of special variables ( Table 9.1 ).

Table 9.1. Special Variables

V ARIABLE

U SE / M EANING

$0

The name with which the script was called on the command line.

$1

The first command-line argument.

$2

The second command-line argument, and so on, up to $9 .

$@

A list of all the command-line arguments.

$#

The number of command-line arguments.

$?

The exit status of the most recent command. ( means success; other numbers mean some kind of error.)


The variable $0 contains the name of the script itself; the first argument to a script can be obtained from $1 , the second argument from $2 , and so on. Arguments with more than one digit must be written with braces around the numberfor example, ${10} , ${25} .

When you supply options to a script, such as -A or -r , the script thinks of them as simply arguments. So in the command line

myscript.sh -A foo

$0 contains myscript.sh , $1 contains -A , and $2 contains foo . $3 contains nothing (because there were only two arguments).

To use command-line arguments in your script:

  • You simply use the special variables as you would any other variable. For example,

    echo "The first argument was $1"

    Figure 9.11 is a code listing that shows the use of the special variables for command-line arguments, and Figure 9.12 shows the output of the script. Notice how the second and third arguments in Figure 9.12 are enclosed in quotes. Without the quotes, only the first three words would have been printed out ( What , a , and long ).

    Figure 9.11. Code listing of a script that uses special variables to access command-line arguments.
     #!/bin/sh # This script uses the special variables for # command line arguments. echo "Hey $USER called me: 
     #!/bin/sh # This script uses the special variables for # command line arguments. echo "Hey $ USER called me: $0" echo "First argument: $1" echo "Second argument: $2" echo "Third argument: $3" 
    " echo "First argument: " echo "Second argument: " echo "Third argument: "

    Figure 9.12. Output from the script shown in Figure 9.11.
     localhost:~ vanilla$  ./args.sh What "a long strange trip" "it's been"  Hey vanilla called me: ./args.sh First argument: What Second argument: a long strange trip Third argument: it's been localhost:~ vanilla 

There are a couple of ways to handle large numbers of arguments. The first is to use the shift command. The shift command (which typically appears on a line all by itself) moves all of the arguments "down" one step. That is, after you use shift , $1 contains what used to be in $2 , $2 contains what used to be in $3 , and so on.

One good reason to use shift is if you do not know how many arguments will be passed to your script . First check to see if $1 contains anything each time you call shift . As long as it does, you know you have (at least) one more argument to process. Alternatively, you can use the special variable $@ , which contains a list of all the arguments. By using $@ in combination with a loop (see "Using loops ," later in the chapter), it is possible to iterate over (a programming term for running through ) all of the script's arguments, regardless of how many there are.

To use shift to access arguments:

1.
Put the shift command on a line by itself.

For example,

 echo "First argument was: " echo "Ninth argument was: " shift 

2.
Use any of the special variables, such as $1 or $2 . For example,

 echo "Here's one: " echo "And here's nine: " 

After shift is used, all the arguments are "shifted" over, so if you think of the list of arguments as something like $1 , $2 , $3 , $4 , $5 , $6 , $7 , $8 , $9 , you can picture using shift to move all the arguments one place to the left.

The contents of $1 disappear (they do not go into $0 ), and whatever was in ${10} goes into $9 . For example, if before shift is used, $0 contained myscript.sh , $1 contained -A , $2 contained 100 , and $3 was empty, then after the shift statement $0 would still contain myscript.sh , $1 would now contain 100 , and $2 would be empty.

You can use shift as many times as you like in your script.

To refer to the entire list of arguments:

  • Use the special variable $@ .

    The special variable $@ contains all of the command-line arguments (only the argumentsit does not contain $0 ). You can use it to pass all the arguments your script received to a command inside your script. For example,

    ls "$@"

    Figure 9.13 is a code listing of a script that is a simple replacement for the rm command. Instead of deleting the files named in its arguments, this script uses mv to move them all into the user's Trash (the .trash directory in the user's home directory). See the sidebar "Compare with Aqua: Using the Trash from the Command Line," on the next page, for some important differences between moving files with this script and actually dragging a file to the Trash in the Finder. Figure 9.14 shows an example of using this script, including how to see the contents of the Trash from the command line. Notice how you can use command-line wildcards with the script. The argument badfile* in Figure 9.14 ended up matching two files: badfile1 and badfile2 , and all four arguments were captured in $@ and passed to the mv command in the script.

    Figure 9.13. Code listing of a script that moves files to the Trash from the command line, using $@ to pass all the command-line arguments.
     #!/bin/sh # trash - script for moving files to the Trash # from the command line. # Each user's Trash is a directory called .Trash in their # home directory trash_directory="$HOME/.Trash/" # We use $@ to pass all the arguments mv "$@" "$trash_directory" 

    Figure 9.14. Example of using the script in Figure 9.13.
     localhost:~ vanilla$  ./trash junkfile file-o-junk badfile*  localhost:~ vanilla$ ls  .Trash badfile1    badfile2     file-o-junk    junkfile [localhost:~] 

    Figure 9.15 shows the Finder view of the Trash folder after using the script.

    Figure 9.15. The Finder view of the Trash folder after using the script for moving files to the Trash.


Tip

  • Create this script with the name "trash," and follow the instruction in "Running a script without using a path " at the beginning of this chapter. This script is one you could use every day.


Compare with Aqua: Using the Trash from the Command Line

The script in Figure 9.13 allows you to move files to the Trash instead of using the Unix rm command, but there are some important differences between the behavior of this script and the way the Finder uses the Trash.

When you drag a file to the Trash, a check is performed in the Finder to see if a file with the same name already exists in the Trash. If so, the new file is renamed by adding a 1 at the end of its name, or a 2 if this is the second duplicate, and so on. There can be only one file with any particular name inside a folder (or directory).

The Finder won't simply replace the older file, since the whole point of the Trash is that using it is not the same as actually deleting a file.

The script shown in Figure 9.13 is not as sophisticated as the Finder. If a file with the same name as an argument has already been moved to the Trash, this script will overwrite the older file.

Later in this chapter we will show you how to improve the script to make its behavior more like the Finder's use of the Trash.




Unix for Mac OS X 10. 4 Tiger. Visual QuickPro Guide
Unix for Mac OS X 10.4 Tiger: Visual QuickPro Guide (2nd Edition)
ISBN: 0321246683
EAN: 2147483647
Year: 2004
Pages: 161
Authors: Matisse Enzer

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