< Day Day Up > |
11.2. The Environment11.2.1 The Initialization FilesAfter executing the commands in /etc/profile, the initialization files in the user's home directory are executed. The .profile is executed, followed by the ENV file, commonly called the .kshrc file. The /etc/profile FileThe /etc/profile is a systemwide readable file set up by the system administrator to perform tasks when the user logs on and the Korn 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 text is an example of the /etc/profile. See Chapter 8, "Programming the Bourne Shell," on page 321 for a complete explanation of each line of /etc/profile. Example 11.1.# The profile that all logins get before using their own .profile trap " " 2 3 export LOGNAME PATH # Initially set by /bin/login if [ "$TERM" = " " ] then if /bin/i386 then # Set the terminal type TERM=AT386 else TERM=sun fi export TERM fi # Login and -su shells get /etc/profile services. # -rsh is given its environment in the .profile. case "$0" in -sh | -ksh | -jsh ) if [ ! -f .hushlogin ] then /usr/sbin/quota # Allow the user to break the Message-Of-The-Day only. trap "trap ' ' 2" 2 /bin/cat -s /etc/motd # Display the message of the day trap " " 2 /bin/mail -E case $? in 0) # Check for new mail echo "You have new mail. " ;; 2) echo "You have mail. " ;; esac fi esac umask 022 trap 2 3 The .profile FileThe .profile file is a user-defined initialization file that is executed once at login (by the Bourne and Korn shells) and is found in your home directory. It gives you the ability to customize and modify your working environment. Environment variables and terminal settings are normally set here, and if a window application or dbm is to be initiated, it is started here. If the .profile file contains a special variable called ENV , the filename that is assigned to that variable will be executed next. The ENV file is often named .kshrc; it contains aliases and set –o commands. The ENV file is executed every time a ksh subshell is spawned. The lines from the following files may not be meaningful to you now, but all of the concepts, such as exporting variables, history, the search path, and so on, will be discussed in detail throughout the text of this book. Example 11.2.1 set -o allexport 2 TERM=vt102 3 HOSTNAME=$(uname -n) 4 HISTSIZE=50 5 EDITOR=/usr/ucb/vi 6 ENV=$HOME/.kshrc 7 PATH=$HOME/bin:/usr/ucb:/usr/bin:\ /usr/local:/etc:/bin:/usr/bin:/usr/local\ /bin:/usr/hosts:/usr/5bin:/usr/etc:/usr/bin:. 8 PS1="$HOSTNAME ! $ " 9 set +o allexport 10 alias openwin=/usr/openwin/bin/openwin 11 trap '$HOME/.logout' EXIT 12 clear EXPLANATION
The ENV FileThe ENV variable is assigned the name of a file that will be executed every time an interactive ksh or a ksh program (script) is started. The ENV variable is set in the .profile and is assigned the name of the file that will contain special ksh variables and aliases. The name is conventionally .kshrc, but you can call it anything you want. (The ENV file is not processed when the privileged option is on. See Table 11.1.) Example 11.3.1 $ export ENV=.kshrc 2 $ cat .kshrc # The .kshrc file 3 set -o trackall 4 set -o vi 5 alias l='ls -laF' alias ls='ls -aF' alias hi='fc -l' alias c=clear 6 function pushd { pwd > $HOME/.lastdir.$$ ; } function popd { cd $(< $HOME/.lastdir.$$) ; rm $HOME/.lastdir.$$; pwd; } function psg { ps -ef | egrep $1 | egrep -v egrep; } function vg { vgrind -s11 -t $* | lpr -t ; }
EXPLANATION
The set –o OptionsThe set command, used by itself, sets positional parameters (see "Positional Parameters" on page 633), but with the –o switch, it can be used to turn on and turn off ksh options that allow you to customize the shell environment. The options are either on or off, and are normally set in the ENV file. (See Table 11.1 for a list of shell set options.) FORMAT set -o option Turns on the option. set +o option Turns off the option set -[a-z] Abbreviation for an option; the minus turns it on set +[a-z] Abbreviation for an option; the plus turns it off Example 11.4.1 set -o allexport 2 set +o allexport 3 set -a 4 set +a EXPLANATION
11.2.2 The PromptsThe Korn shell provides four prompts. The primary and secondary prompts are used when the Korn shell is running interactively. You can change these prompts. The variable PS1 is the primary prompt, set initially to a dollar sign ($). It appears when you log on and waits for you to type commands. The variable PS2 is the secondary prompt, initially set to the > character. It appears if you have partially typed a command and then pressed Enter. You can change the primary and secondary prompts. The Primary Prompt$ is the default primary prompt. You can change your prompt. Normally, prompts are defined in .profile. Example 11.5.1 $ PS1="$(uname –n) ! $ " 2 jody 1141 $ EXPLANATION
The Secondary PromptThe PS2 prompt is the secondary prompt. Its value is displayed to standard error (the screen). This prompt appears when you have not completed a command and have pressed the carriage return. Example 11.6.1 $ print "Hello 2 > there" 3 Hello there 4 $ 5 $ PS2="––––> " 6 $ print "Hi 7 ––––––> ––––––> ––––––> there" Hi there $ EXPLANATION
11.2.3 The Search PathTo execute a command typed at the command line or within a shell script, the Korn shell searches the directories listed in the PATH variable. The PATH is a colon-separated list of directories, searched by the shell from left to right. The dot in the PATH represents the current working directory. If the command is not found in any of the directories listed in the path, the shell sends the message ksh: filename: not found to standard error. It is recommended that the path be set in the .profile file. To speed up the searching process, the Korn shell has implemented tracked aliases. See "Tracked Aliases" on page 606. Example 11.7.$ echo $PATH /home/gsa12/bin:/usr/ucb:/usr/bin:/usr/local/bin:. EXPLANATION The Korn shell will search for commands starting in /home/gsa12/bin. If the command is not found there, /usr/ucb is searched, then /usr/bin, /usr/local/bin, and finally the user's current working directory represented by the period. 11.2.4 The dot CommandThe dot command (.) is a built-in Korn shell command. It takes a script name as an argument. The script will be executed in the environment of the current shell. A child process will not be started. The dot command is normally used to re-execute the .profile file or the ENV file, if either of those files has been modified. For example, if one of the settings in either file has been changed after you have logged on, you can use the dot command to re-execute the initialization files without logging out and then logging back in. Example 11.8.$ . .profile $ . .kshrc $ . $ENV EXPLANATION Normally a child process is started when commands are executed. The dot command executes each of the initialization files, .profile and the ENV file (.kshrc), in the current shell. Local and global variables in these files are defined within this shell. Otherwise, the user would have to log out and log back in to cause these files to be executed for the login shell. The dot command makes that unnecessary. |
< Day Day Up > |