< Day Day Up > |
7.2. The EnvironmentThe environment of a process consists of variables, open files, the current working directory, functions, resource limits, signals, and so forth. It defines those features that are inherited from one shell to the next and the configuration for the working environment. The configuration for the user's shell is defined in the shell initialization files. 7.2.1 The Initialization FilesAfter the Bourne shell program starts up, it first checks for the system file /etc/profile. After executing the commands in that initialization file, .profile in the user's home directory is executed. Skeleton files for initial setup can be found in /etc/skel (SVR4). The /etc/profile FileThe /etc/profile file is a systemwide initialization file set up by the system administrator to perform tasks when the user logs on. It is executed when the Bourne shell starts up. It is available to all Bourne and Korn shell users on the system and normally performs such tasks as checking the mail spooler for new mail and displaying the message of the day from the /etc/motd file. (The following examples will make more sense after you have completed this chapter.) Example 7.1.(Sample /etc/profile) # The profile that all logins get before using their own .profile 1 trap " " 2 3 2 export LOGNAME PATH 3 if [ "$TERM" = " " ] then if /bin/i386 then TERM=AT386 # Sets the terminal else TERM=sun fi export TERM fi # Login and -su shells get /etc/profile services. # -rsh is given its environment in its own .profile. 4 case "$0" in -sh | -ksh | -jsh ) 5 if [ ! -f .hushlogin ] then /usr/sbin/quota # Allow the user to break the Message-Of-The-Day only. 6 trap "trap ' ' 2" 2 7 /bin/cat -s /etc/motd # Message of the day displayed trap " " 2 8 /bin/mail -E # Checks for new mail 9 case $? in 0) echo "You have new mail. " ;; 2) echo "You have mail. " ;; esac fi esac 10 umask 022 11 trap 2 3 EXPLANATION
The .profile FileThe .profile file is a user-defined initialization file executed once at login and found in your home directory. It gives you the ability to customize and modify the shell environment. Environment and terminal settings are normally put here, and if a window application or database application is to be initiated, it is started here. The settings in this file will be discussed in detail as the chapter progresses, but a brief synopsis of each line in the file is explained here. Example 7.2.
(Sample .profile)
1 TERM=vt102
2 HOSTNAME=`uname -n`
3 EDITOR=/usr/ucb/vi
4 PATH=/bin:/usr/ucb:/usr/bin:/usr/local:/etc:/bin:/usr/bin:.
5 PS1="$HOSTNAME $ > "
6 export TERM HOSTNAME EDITOR PATH PS1
7 stty erase ^h
8 go () { cd $1; PS1=`pwd`; PS1=`basename $PS1`; }
9 trap '$HOME/.logout' EXIT
10 clear
EXPLANATION
7.2.2 The PromptsWhen used interactively, the shell prompts you for input. When you see the prompt, you know that you can start typing commands. The Bourne shell provides two prompts: the primary prompt, a dollar sign ($), and the secondary prompt, a right angle bracket symbol (>). The prompts are displayed when the shell is running interactively. You can change these prompts. The variable PS1 is the primary prompt set initially to a dollar sign. The primary prompt appears when you log on and the shell waits for you to type commands. The variable PS2 is the secondary prompt, initially set to the right angle bracket character. It appears if you have partially typed a command and then pressed the carriage return. You can change the primary and secondary prompts. The Primary PromptThe dollar sign is the default primary prompt. You can change your prompt. Normally prompts are defined in .profile, the user initialization file. Example 7.3.1 $ PS1="`uname -n > `" 2 chargers > EXPLANATION
The Secondary PromptThe PS2 prompt is the secondary prompt. Its value is displayed to standard error, which is the screen by default. This prompt appears when you have not completed a command and have pressed the carriage return. Example 7.4.1 $ echo "Hello 2 > there" 3 Hello there 4 $ 5 $ PS2="末末> " 6 $ echo 'Hi 7 末末> 末末> 末末> there' Hi there $ EXPLANATION
7.2.3 The Search PathThe PATH variable is used by the Bourne shell to locate commands typed at the command line. The path is a colon-separated list of directories used by the shell when searching for commands. The search is from left to right. The dot at the end of the path represents the current working directory. If the command is not found in any of the directories listed in the path, the Bourne shell sends to standard error the message filename: not found. It is recommended that the path be set in the .profile file. If the dot is not included in the path and you are executing a command or script from the current working directory, the name of the script must be preceded with a ./, such as ./program_name, so that shell can find the program. Example 7.5.(Printing the PATH) 1 $ echo $PATH /home/gsa12/bin:/usr/ucb:/usr/bin:/usr/local/bin:/usr/bin:/usr/local/bin:. (Setting the PATH) 2 $ PATH=$HOME:/usr/ucb:/usr:/usr/bin:/usr/local/bin:. 3 $ export PATH EXPLANATION
7.2.4 The hash CommandThe hash command controls the internal hash table used by the shell to improve efficiency in searching for commands. Instead of searching the path each time a command is entered, the first time you type a command, the shell uses the search path to find the command, and then stores it in a table in the shell's memory. The next time you use the same command, the shell uses the hash table to find it. This makes it much faster to access a command than having to search the complete path. If you know that you will be using a command often, you can add the command to the hash table. You can also remove commands from the table. The output of the hash command displays both the number of times the shell has used the table to find a command (hits) and the relative cost of looking up the command (cost), that is, how far down the search path it had to go before it found the command. The hash command with the 睦 option clears the hash table. Example 7.6.1 $ hash hits cost command 3 8 /usr/bin/date 1 8 /usr/bin/who 1 8 /usr/bin/ls 2 $ hash vi 3 8 /usr/bin/date 1 8 /usr/bin/who 1 8 /usr/bin/ls 0 6 /usr/ucb/vi 3 $ hash 睦 EXPLANATION
7.2.5 The dot CommandThe dot command is a built-in Bourne shell command. It takes a script name as an argument. The script will be executed in the environment of the current shell; that is, a child process will not be started. All variables set in the script will become part of the current shell's environment. Likewise, all variables set in the current shell will become part of the script's environment. The dot command is normally used to re-execute the .profile file if it has been modified. For example, if one of the settings, such as the EDITOR or TERM variable, has been changed since you logged on, you can use the dot command to re-execute the .profile without logging out and then logging back in. Example 7.7.
$ . .profile
EXPLANATION The dot command executes the initialization file, .profile, within this shell. Local and global variables are redefined within this shell. The dot command makes it unnecessary to log out and then log back in again.[a]
|
< Day Day Up > |