Using Command-Line Arguments


You can pass command-line arguments to your scripts. When you execute a script, shell variables are automatically set to match the arguments. These variables are referred to as positional parameters. The parameters $1, $2, $3, $4 (up to $9) refer to the first, second, third, fourth (and so on) arguments on the command line. The parameter $0 is the name of the shell program itself.

Shell Positional Parameters

Command

arg1

arg2

arg3

arg4

arg5

arg9

|

|

|

|

|

|

 

|

$0

$1

$2

$3

$4

$5

$9

The parameter $# is the total number of arguments passed to the script. The parameter $* refers to all of the command-line arguments (not including the name of the script). The parameter $@ is sometimes used in place of $*; for the most part, they mean the same thing, although they behave slightly differently when quoted.

To see the relationships between words entered on the command line and variables available to a shell program, create the following sample shell program:

 $ cat show_args echo You ran the program called $0 echo with the following arguments: echo $1 echo $2 echo $3 echo Here are all $# arguments: echo $*

The output of this script could look like this:

 $ chmod +x show_args $ ./show_args This is a test of show_args with 11 command line arguments You ran the program called ./show_args with the following arguments: This is a Here are all 11 arguments: This is a test of show_args with 11 command line arguments

The variable $* is especially useful because it allows your scripts to accept an arbitrary number of command-line arguments. For example, the backupWork script can be generalized to back up any files specified on the command line. In this example, the positional parameters are also used to add information to the e-mail sent by backupWork.

 #!/bin/sh # backupWork-a program to backup any files and #            directories given as command line arguments # Version 2, Sept 2006 # Get the current date in a special format # Create the new backup directory TIMESTAMP='date +%Y.%m.%d.%T' BACKUPDIR="~/Backups/Backup.$TIMESTAMP" mkdir $BACKUPDIR # Copy files in command line arguments to new directory cp -r $* $BACKUPDIR # Send mail to confirm that backup was done # Include name of script and all command line arguments in the mail echo "Running the script $0 $*" > mailmsg echo "Backup to $BACKUPDIR completed." >> mailmsg mail $LOGNAME < mailmsg rm mailmsg

Shifting Positional Parameters

You can reorder positional parameters with the built-in shell command shift. This removes the first argument from $* and takes $# down by 1. It also renames the parameters, changing $2 to $1, $3 to $2, $4 to $3, and so forth. The original value of $1 is lost. (The value of $0 is unchanged.)

The following example illustrates the use of shift to manage positional parameters. The first argument to quickmail must be an e-mail address. The second argument is the (one-word) subject, and the remaining arguments are the contents of the e-mail.

 #!/bin/sh # quickmail-send mail from the command line #   useage: quickmail recipient subject contents RECIPIENT=$1 SUBJECT=$2 shift/shift (echo $*) mail $RECIPIENT -s $SUBJECT echo $# word message sent to $RECIPIENT.

In this script, the first two arguments are saved in the variables RECIPIENT and SUBJECT. The two shift commands then move the list of positional parameters by two items; after the shift commands, $1 is the third word of the original command-line arguments. All of the remaining arguments are sent to mail on standard input (as the output of the echo command). Here’s what quickmail might look like when run:

 $ ./quickmail jcm homework When will you hand out the next assignment? 8 word message sent to jcm.

The set Command

The shell command set takes a string and assigns each word to one of the positional parameters. (Any command-line arguments that are stored in the positional parameters will be lost.) For example, you could assign all the list of files in the current directory to the variables $1, $2, etc., with

 set * echo "There are $# files in the current directory."

You may recall from Chapter 4 that backquotes can be used to perform command substitution. You can use this to set the positional parameters to the output of a command. For example,

 $ set 'date' $ echo $* Sun Dec 30 12:55:14 PST 2006 $ echo "$1, the ${3}th of $2" Sun, the 30th of Dec $ echo $6 2006




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

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