7.2. The Environment

 <  Day Day Up  >  

The 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 Files

After 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 File

The /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 " 
 (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 
" 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

  1. The trap command controls signals coming into this program while it is running. If signals 2 (Ctrl-C) or 3 (Ctrl-\) are sent while the program is in execution, those signals will be ignored.

  2. The variables LOGNAME and PATH are exported so that their values will be known in subshells started from this process.

  3. The command /bin/i386 is executed. If the exit status of the command is 0, the terminal variable, TERM , is assigned the value AT386 ; if not, the TERM variable is assigned sun .

  4. If the value of $0 , the name of the program running the /etc/profile file, is either a login Bourne, Korn, or job shell, the following commands will be executed.

  5. If the .hushlogin file does not exist, quota will be run to display the disk usage warnings if usage is over the quota.

  6. The trap is reset so that the user can terminate the message of the day (motd) with Ctrl-C.

  7. After the message of the day has been displayed, the trap is reset to ignore Ctrl-C.

  8. The mail program checks for new incoming mail.

  9. If the exit status ($?) of the mail program is or 2 , the message You have new mail. or You have mail. , respectively, is displayed.

  10. The umask command is set to determine the initial permissions of files and directories when they are created.

  11. The trap command sets signals 2 and 3 back to their defaults; that is, to kill the program if either Ctrl-C or Ctrl-\ arrive .

The .profile File

The .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 ; PS1=`pwd`; PS1=`basename $PS1`; } 9   trap '$HOME/.logout' EXIT 10  clear 

EXPLANATION

  1. The TERM variable is assigned the value of the terminal type, vt102 .

  2. Because the uname “n command is enclosed in backquotes, the shell will perform command substitution; that is, the output of the command (the name of the host machine) will be assigned to the variable HOSTNAME .

  3. The EDITOR variable is assigned /usr/ucb/vi . Programs such as mail will now have this variable available when defining an editor.

  4. The PATH variable is assigned the directory entries that the shell searches to find a UNIX/Linux program. If, for example, you type ls , the shell will search the PATH until it finds that program in one of the listed directories. If it never finds the program, the shell will tell you so.

  5. The primary prompt is assigned the value of HOSTNAME , the machine name, and the $ and > symbols.

  6. All of the variables listed are exported. They will be known by child processes started from this shell.

  7. The stty command sets terminal options. The erase key is set to ^h , so that when you press the Backspace key, the letter typed preceding the cursor is erased.

  8. A function called go is defined. The purpose of this function is to take one argument, a directory name, cd to that directory, and set the primary prompt to the present working directory. The basename command removes all but the last entry of the path. The prompt will show you the current directory.

  9. The trap command is a signal handling command. When you exit the shell, that is, log out, the . logout file will be executed.

  10. The clear command clears the screen.

7.2.2 The Prompts

When 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 Prompt

The 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

  1. The default primary prompt is a dollar sign ($) . The PS1 prompt is being reset to the name of the machine (uname “n) and a > symbol. (Don't confuse backquotes and single quotes.)

  2. The new prompt is displayed.

The Secondary Prompt

The 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

  1. The double quotes must be matched after the string "Hello .

  2. When a newline is entered, the secondary prompt appears. Until the closing double quotes are entered, the secondary prompt will be displayed.

  3. The output of the echo command is displayed.

  4. The primary prompt is displayed.

  5. The secondary prompt is reset.

  6. The single quote must be matched after the string 'Hi .

  7. When a newline is entered, the new secondary prompt appears. Until the closing single quote is entered, the secondary prompt will be displayed.

7.2.3 The Search Path

The 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

  1. By echoing $PATH , the value of the PATH variable is displayed. The path consists of a list of colon-separated elements and is searched from left to right. The dot at the end of the path represents the user's current working directory.

  2. To set the path, a list of colon-separated directories are assigned to the PATH variable.

  3. By exporting the path, child processes will have access to it.

7.2.4 The hash Command

The 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 “r 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 r  

EXPLANATION

  1. The hash command displays the commands currently stored in the internal hash table. The shell will not have to search the search path to find the commands listed when they are entered at the command line. This saves time. Otherwise, the shell has to go out to the disk to search the path. When you type a new command, the shell will search the path first, and then place it on the hash table. The next time you use that command, the shell finds it in memory.

  2. The hash command can take arguments; the names of commands you want to guarantee get stored on the hash table ahead of time.

  3. The hash command with the “r option clears the hash table.

7.2.5 The dot Command

The 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]

[a] If the . profile were executed directly as a script, a subshell would be started. Then the variables would be set in the subshell, but not in the login shell (parent shell).

 <  Day Day Up  >  


UNIX Shells by Example
UNIX Shells by Example (4th Edition)
ISBN: 013147572X
EAN: 2147483647
Year: 2004
Pages: 454
Authors: Ellie Quigley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net