Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Hour 12
Parameters

As you saw in previous chapters, the general format for the invocation of programs in UNIX is

command options files

Here command is the command name, options is any option that you need to specify, and files is an optional list of files on which the command should operate. Consider the following example:

$ ls –l *.doc

Here ls is the command, –l is the only option, and *.doc is the list of files for ls to operate on.

Because most UNIX users are familiar with this interface, you should adhere to this format in shell scripts. This means that scripts that can have options specified must be able to read and interpret them correctly.

You have two common methods for the handling options passed to a shell script:

  Handle options manually using a case statement
  Handle options using the getopts command

For scripts that support only one or two options, the first method is easy to implement and works quite well, but many scripts allow any combination of several options to be given. For such scripts, the getopts command is very useful because it affords the maximum flexibility in parsing options.

This chapter looks at both methods but it first covers the topic of special shell variables.

Special Variables

The shell defines several special variables that are relevant to option parsing. In addition to these, a few variables give the status of commands that the script executes. Table 12.1 describes all of the special variables defined by the shell.

In this section you construct a simple yet useful shell script that illustrates the use of these variables.

Table 12.1 Special Shell Variables

Variable Description

$0 The name of the command being executed. For shell scripts, this is the path with which it was invoked.
$n These variables correspond to the arguments with which a script was invoked. Here n is a positive decimal number corresponding to the position of an argument (the first argument is $1, the second argument is $2, and so on).
$# The number of arguments supplied to a script.
$* All the arguments are double quoted. If a script receives two arguments, $* is equivalent to $1 $2.
$@ All the arguments are individually double quoted. If a script receives two arguments, $@ is equivalent to $1 $2.
$? The exit status of the last command executed.
$$ The process number of the current shell. For shell scripts, this is the process ID under which they are executing.
$! The process number of the last background command.

Using $0

Start by looking at $0. This variable is commonly used to determine the behavior of scripts that can be invoked with more than one name. Consider the following script:

#!/bin/sh
case $0 in
    *listtar) TARGS="–tvf $1" ;;
    *maketar) TARGS="–cvf $1.tar $1" ;;
esac
tar $TARGS

You can use this script to list the contents of a tar file (t as in tape and ar as in archive, a common format for distributing files in UNIX) or to create a tar file based on the name with which the script is invoked. The tar file to read or create is specified as the first argument, $1.

I called this script mytar and made two symbolic links to it called listtar and maketar as follows:

$ ln –s mytar listtar
$ ln –s mytar maketar

If the script is invoked with the name maketar and is given a directory or filename, a tar file is created. If you had a directory called fruits with the following contents

$ ls fruits
apple   banana  mango   peach   pear

you can invoke the script as maketar to obtain a tar file called fruit.tar containing this directory, by issuing the following command:

$ ./maketar fruits

If you want to list the contents of this tar file, you can invoke the script as follows:

$ ./listtar fruits.tar

This gives us the following output:

rwxr-xr-x 500/100      0 Nov 17 08:48 1998 fruits/
rw-r--r-- 500/100      0 Nov 17 08:48 1998 fruits/apple
rw-r--r-- 500/100      0 Nov 17 08:48 1998 fruits/banana
rw-r--r-- 500/100      0 Nov 17 08:48 1998 fruits/mango
rw-r--r-- 500/100      0 Nov 17 08:48 1998 fruits/pear
rw-r--r-- 500/100      0 Nov 17 08:48 1998 fruits/peach

For this example, the output that you encounter depends on the version of tar that is installed on your machine. Some versions include more detail in the output than are shown here.


Previous Table of Contents Next