-->
Previous | Table of Contents | Next |
Using Command-Line Parameters
When the shell interprets a command, it attaches variable names to each item on the command line. The items on the command line are the sequences of characters separated by blanks or tab characters. (Use quotation marks to signal that a collection of characters separated by spaces represents one item.) The variables attached to the items in the command line are $0, $1, $2, and so on through $9. These 10 variables correspond to the positions of the items on the line. The command name is $0, the first argument or parameter for the command is $1, and so on. To demonstrate this concept, consider the following sample shell script named shovars:
# Name: shovars # Purpose: demonstrate command-line variables echo $0 echo $2 $4! echo $3
Now suppose that you enter this command:
shovars -s hello look at me bart
The output of the shell script is this:
shovars hello bart! look at me
In this output, the first line is the commands name (variable $0), the second line is the second and fourth arguments (variables $2 and $4), and the last line is the third argument (variable $3).
Following is a more serious example. This shell script deletes a file but first copies it to the directory /tmp so that you can retrieve it if necessary:
# Name: safrm # Purpose: copy file to directory /tmp and then remove it # from the current directory # first copy $1 to /tmp cp $1 /tmp # now remove the file rm $1
If you enter safrm abc def, only the file abc is removed from the current directory because the safrm shell script deletes only variable $1. You can, however, represent all the parameters on the command line with $*. Make safrm more general by replacing each occurrence of $1 with $*. If you then enter safrm abc def xx guio, all four files (abc, def, xx, and guio) are removed from the current directory.
Substituting the Output of a Command
You can assign to a variable the result of an executed command. To store the name of the current working directory in a variable named cwd, for example, enter this:
cwd= pwd
Notice that pwd, the print working directory command, is set in backquotes instead of single quotation marks.
The following shell script changes the name of a file by appending the current month, day, and year to the filename:
# Name: stamp # Purpose: rename file: append todays date to its name # set td to current date in form of mmddyy td=+%m%d%y # rename file mv $1 $1.$td
In this example, the variable td is set to the current date. In the final line, this information is appended to variable $1. If today is February 24, 1997, and you use this script on a file called myfile, the file is renamed (moved) to myfile.022497.
Using Special Characters in Shell Programs
Youve seen how the shell gives special treatment to certain characters, such as >, *, ?, $, and others. What do you do if you dont want those characters to get special treatment? This section provides a few answers.
You can use the single quote to make the shell ignore special characters. Enclose the character string with a pair of single quotes, as in this example:
grep ^Mary Tuttle customers
The result of this grep command is that the lines in the file customers that begin with Mary Tuttle are displayed. The caret (^) tells grep to search from the beginning of the line. If the text Mary Tuttle wasnt enclosed in single quotes, it might be interpreted literally (or as a pipe symbol on some systems). Also, the space between Mary and Tuttle isnt interpreted by the shell when it occurs within the single quotes.
You can also use quotation marks to make the shell ignore most special characters, with the exception of the dollar sign and backquote. In the following example, the asterisks, spaces, and the greater-than sign are treated as regular characters because the string is surrounded by quotation marks:
echo ** Please enter your response >
In this next example, however, $LOGNAME evaluates correctly, but theres no value for $5:
echo >>>Thanks for the $5, $LOGNAME
Use the backslash (\) to make the shell ignore a single character. For example, to make the shell ignore the dollar sign in front of the 5, issue this command:
echo >>>Thanks for the \$5, $LOGNAME
The result is what you expect:
>>>Thanks for the $5, wrev
There are two primary control structures in shell programming: decision structures and iterative structures. In decision structures, such as if then else and case, you can have the shell script decide which commands to execute based on the value of an expression (such as a variable, the properties associated with a file, the number of parameters in a script, or the result of executing a command). In iterative structures, such as for and while loops, you can execute a sequence of commands over a collection of files or while some condition holds.
The following sections use examples that arent too complicated, yet demonstrate the essentials of programming with some control.
Previous | Table of Contents | Next |