NEXT UP previous
Next: Login Script

Shell Script Parameters

The dirsize shell script we wrote earlier has one major problem: it will only give a count of the number of files in the directory /usr/bin as this pathname is built into the script. It would be much more useful if it were possible to specify the pathname of the directory to use, so that you could find the number of files in any given directory. Ideally, then, you would want to be able to give a command like the following:

	$ dirsize /etc

and have dirsize count the number of files in that directory instead.

In fact, this is quite easy, because the shell automatically splits up any command line you enter into separate words, and then assigns the words to a set of special variables, whose values can be read inside a shell script. The values of these special variables can be accessed with the notation $0, $1, $2, and so on. The value in $0 will be the first word on the line (i.e. the command name), the value in $1 will be the first command line parameter, $2 will be the sccond command line parameter etc. The values starting with $1, $2, etc., are called positional parameters. For example, if a shell script named test1 was executed as follows:

	$ test1 param1 param2

then inside the shell script, the values of the special variables would be:

	$0 = testi
	$1 = parami
	$2 = param2

Given this information, it is now a simple matter to modify dirsize to perform as required:

	$ cat >dirsize
	ls $1 | wc -w
	Ctrl-d
	$ chmod 700 dirsize
	$ dirsize /etc
          69

As you can see, when the shell script is run, the parameter (/etc) is substituted into the ls command in the pipeline, in place of the $1. This substitution is done by the shell and the ls command has no idea that it has taken place.

In order that you can access this new command wherever you are in the directory hierarchy, you need to make sure that the directory which contains the command is listed in your PATH variable. The next example creates a bin directory under your home directory, moves the dirsize command into it and adds this bin directory to the start of your PATH so that the directory will be searched automatically by the shell whenever you enter a command name. The value of the shell variable HOME is used in the example, as it contains the pathname of your home directory:

	$ echo $PATH
	/bin:/usr/bin:.
	$ mkdir $HOME/bin
	$ mv dirsize $HOME/bin
	$ PATH=$HOME/bin:$PATH
	$ echo $PATH
	/usr1/pc/bin:/bin:/usr/bin:.

Remember that the list of directories contained in the PATH variable are separated by colons. The initial contents of PATH show that three directories will be searched for any commands you enter: /bin first, then /usr/bin and, finally, dot, the current directory. The command sequence then modifies the value of PATH by adding /usr1/pc/bin: onto the front of the PATH, so that this directory is searched before the other three, whenever a command is entered.

An alternative notation which you can use when you want to refer to your home directory, in place of $HOME, is to use the tilde (~) symbol. Using this notation $HOME/bin is the same as ~/bin. The tilde notation can also be extended by following it with the login name of another user. In this case the notation expands to be the pathname of the home directory of the specified user. For example:

	$ ls ~carey/bin

would list the contents of bin under the home directory of user carey, assuming that I have permission to read the contents of this directory.


NEXT UP previous
Next: Login Script