Previous Section  < Day Day Up >  Next Section

11.2. The Environment

11.2.1 The Initialization Files

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

The /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 File

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

  1. By setting the allexport option, all variables created will automatically be exported (made available to subshells).

  2. The terminal is set to vt102.

  3. The variable HOSTNAME is assigned the name of this machine, $(uname –n).

  4. The HISTSIZE variable is assigned 50; 50 lines from the history file will be displayed on the terminal when the user types history.

  5. The EDITOR variable is assigned the pathname for the vi editor. Programs such as mail allow you to select an editor in which to work.

  6. The ENV variable is assigned the path to the home directory ($HOME) and the name of the file that contains further Korn shell customization settings. After the .profile is executed, the ENV file is executed. The name of the ENV file is your choice; it is commonly called .kshrc.

  7. The search path is defined. It is a colon-separated list of directories used by the shell in its search for commands typed at the prompt or in a script file. The shell searches each element of the path from left to right for the command. The dot at the end represents the current working directory. If the command cannot be found in any of the listed directories, the shell will look in the current directory.

  8. The primary prompt, by default a dollar sign ($), is set to the name of the host machine, the number of the current command in the history file, and a dollar sign ($).

  9. The allexport option is turned off.

  10. An alias is a nickname for a command. The alias for openwin is assigned the full pathname of the openwin command, which starts Sun's window application.

  11. The trap command will execute the .logout file when you exit this shell, that is, when you log out. The .logout file is a user-defined file containing commands that are executed at the time of logging out. For example, you may want to record the time you log out, clean up a temporary file, or simply say So long.

  12. The clear command clears the screen.

The ENV File

The 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 ; }


Table 11.1. Korn Shell set Options

Name of Option

Abbreviation

What It Does

allexport

–a

Causes set variables to be automatically exported.

bgnice

 

Runs background jobs with a lower priority.

emacs

 

For command-line editing, uses the emacs built-in editor.

errexit

–e

If a command returns a nonzero exit status (fails), executes the ERR trap, if set, and exits. Not set when reading initialization files.

gmacs

 

For command-line editing, uses the gmacs built-in editor.

ignoreeof

 

Prevents logout with ^D; must type exit to exit the shell.

markdirs

 

Puts a trailing backslash (/) on directory names when filename expansion is used.

monitor

–m

Allows job control.

noclobber

 

Protects files from being overwritten when redirection is used.

noexec

–n

Reads commands, but does not execute them. Used to check the syntax of scripts. Not on when running interactively.

noglob

–f

Disables pathname expansion; that is, turns off wildcards.

nolog

 

Does not store function definitions in the history file.

notify

 

Notifies user when background job finishes.

nounset

 

Displays an error when expanding a variable that has not been set.

privileged

–p

When set, the shell does not read the .profile or ENV file; used with setuid scripts.

trackall

 

Enables alias tracking.

verbose

–v

Turns on the verbose mode for debugging.

vi

 

For command-line editing, uses the vi built-in editor.

xtrace

–x

Turns on the echo mode for debugging.


EXPLANATION

  1. The ENV variable is assigned the name of the file that will be exectued every time the Korn shell is invoked. Exporting ENV makes it available to all subshells.

  2. The contents of the .kshrc file are displayed.

  3. The set option for tracked aliases is turned on. (For a complete description, see "Aliases" on page 604.)

  4. The set option for the vi editor is turned on for in-line editing of the history file. (See "Command-Line History" on page 595.)

  5. The aliases (nicknames) for the commands are defined.

  6. The functions are named and defined. (See "Functions" on page 639.)

The set –o Options

The 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

  1. Sets the allexport option. This option causes all variables to be automatically exported to subshells.

  2. Unsets the allexport option. All variables will now be local to the current shell.

  3. Sets the allexport option. Same as 1. Not every option has an abbreviation (see Table 11.1).

  4. Unsets the allexport option. Same as 2.

11.2.2 The Prompts

The 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

  1. The default primary prompt is a $. The PS1 prompt is being reset to the name of the machine $(uname –n), the number of the current history number, and the $. The exclamation point evaluates to the current history number. (To print an exclamation point, type two exclamation points (!!) in the PS1 definition.)

  2. The new prompt is displayed.

The Secondary Prompt

The 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

  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 print command is displayed.

  4. The primary prompt is displayed.

  5. The secondary prompt is reset.

  6. The double quotes must be matched after the string "Hi.

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

11.2.3 The Search Path

To 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 Command

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

    Previous Section  < Day Day Up >  Next Section