-->
Previous | Table of Contents | Next |
The PATH Variable
The PATH variable lists the directories in which the shell searches for commands. The shell searches those directories in the order theyre listed. If PATH=/bin:/usr/bin/:., whenever the shell interprets a command, it first looks in the directory /bin. If it cant find the command there, the shell looks in the directory /usr/bin. Finally, the shell searches the . directory (remember that the dot represents your current directory). When you enter cal to print this months calendar, the shell first looks in /bin. Because the command isnt there, the shell then looks in /usr/bin and finds it.
TIP: If you have a personalized command named cal, the shell never finds it; the shell executes the cal command in /usr/bin first whenever you give the command. Give your commands names that arent the same as system commands.
You may want to put all your shell scripts in one directory and change the PATH variable to include that directory. This arrangement allows you to execute your shell scripts from whatever directory you happen to be in. To do this, follow these steps:
You need to create that new bin directory and modify the PATH variable only once. Under Linux, the directory called /usr/local/bin is created to hold local commands and scripts that arent part of the standard Linux package but that youve added locally and have made available to all users. In this case, you should expect that /usr/local/bin is also part of PATH.
The MAIL Variable
The MAIL variable contains the name of the file that holds your e-mail. Whenever mail comes into the system for you, its put into the file specified by the MAIL variable. If you have a program that notifies you when new mail has arrived, it checks the file associated with the MAIL variable.
The PS1 Variable
The PS1 variable holds the string of characters you see as your primary prompt. The prompt is the string of characters the shell displays whenever its ready to receive a command. You see how you can change this variableand any of the othersin the section Customizing Linux Shells near the end of this chapter.
The TERM Variable
The TERM variable is used to identify your terminal type. Programs that operate in full-screen mode, such as the vi text editor, need this information.
The TZ Variable
The TZ variable holds a string that identifies your time zone. The date program and some other programs require this information.
Your computer system keeps track of time according to Greenwich Mean Time (GMT). If the TZ variable is set to PST8PDT, the time and date are determined as Pacific Standard Time (PST), eight hours west of GMT, with support for Pacific Daylight Savings Time (PDT). Your computer system automatically changes between daylight savings time and standard time.
The LOGNAME Variable
The LOGNAME variable holds your login name, the name or string of characters that the system associates you with. Among the things the LOGNAME variable is used for is to identify you as the owner of your files, as the originator of any processes or programs you may be running, and as the author of mail or messages sent by the write command.
The following example is an extension of safrm, a shell script created for the safe removal of files. The LOGNAME variable is used to remove all the files you own from the directory /tmp. To do that, the shell script uses the find command. The find command has a number of options; the shell script uses this find command line:
find /tmp -user $LOGNAME -exec rm {} \;
The first parameter, /tmp, is the directory to search. The option -user indicates that you want to search for all files that belong to a specified user. Before the command is executed, the shell replaces $LOGNAME with the current users login name. The option -exec indicates that the following command is to be applied to every file found by the find program. In this case, the rm program is used to remove the found files. The braces ({}) represent the position of each filename passed to the rm command. The last two characters, \;, are required by the find command (an example of using the backslash to pass a character on to a program without being interpreted by the shell). Add this command line to the shell script in Listing 18.1 to obtain a program that removes files safely and also cleans up anything a user has in the /tmp directory thats more than 10 days old.
Listing 18.1 The safrm Shell Script
# Name: safrm # Purpose: copy files to directory /tmp, remove them # from the current directory, clean up /tmp, # and finally send mail to user # first copy all parameters to /tmp cp $* /tmp # remove the files rm $* # create a file to hold the mail message # The files name is set to msg # followed by process ID number of this process # For example, msg1208 msgfile=/tmp/msg$$ # construct mail message date > $msgfile echo These files were deleted from /tmp >>$msgfile # get list of files to be deleted from tmp # -mtime +10 gets all files that havent been # modified in 10 or more days, -print displays the names. find /tmp -user $LOGNAME -mtime +10 -print >> $msgfile # remove the appropriate files from /tmp find /tmp -user $LOGNAME -mtime +10 -exec rm {} \; # mail off the message mail $LOGNAME < $msgfile # clean up rm $msgfile
Previous | Table of Contents | Next |