 |
» |
|
|
 |
When the POSIX or Korn Shell is your login shell, it looks
for these following files and executes them, if they exist: - /etc/profile
This default system file is executed by the shell
program and sets up default environment variables. - .profile
If this file exists in your home directory, it is
executed next at login.
At any time—this includes login
time—the POSIX or Korn Shell is invoked, it looks for the
file referenced by the following shell variable, and executes it,
if it exists: - ENV
When you invoke the shell, it looks for a shell
variable called ENV
which is usually set in your .profile.
ENV is evaluated
and if it is set to an existing file, that file is executed. By
convention, ENV
is usually set to .kshrc
but may be set to any file name.
These files provide the means for customizing the shell environment
to fit your needs. Setting up .profile |  |
The shell script, .profile,
sets your environment by defining commands, variables, and parameters
at login. These values, if the variables are exported, are global
and available to subshells and subprocesses. Here is an example
.profile file: PATH=/bin/posix:/usr/bin:/usr/lib:/bin:/users/mary/bin:. MAIL=/usr/mail/mary HOME=/users/mary EDITOR=/usr/bin/vi ENV='${START[ (_$- = 1) + (_ = 0) - (_$- != _${-%%*i*}) ]}' START=~/.kshrc TERM=hp2392 export ENV START EDITOR TERM PATH MAIL HOME stty sane susp ^Z if mail -e then echo "You have mail." fi PS1="$ " |
Each line of the example .profile
file, except the if
statement and the stty
command, shows a POSIX or Korn Shell variable: PATH
defines the search path for the shell to look up commands (executable
programs or utilities) in the system file structure. Each directory
in the path is separated with a colon (:).
When a command is executed, the shell looks in each of the directories
specified in PATH to find the command.
When you type ksh,
the shell checks /usr/bin
first and then /usr/lib
and so on down the PATH
line until it finds the directory where the ksh
program resides. In this instance, ksh
is found in the third directory, /bin. MAIL
names the file in which your mail is delivered. The if
statement checks whether new mail has arrived and notifies you. HOME
sets your home directory to the directory where the shell places
you when you execute the cd
(change directory) command with no options. This is usually set
automatically by the shell at login. EDITOR
sets your default editor to the vi
editor. Then whenever you need to perform inline command changes,
you immediately enter vi
mode. If you have never used the vi
editor, see Using HP-UX, or The
Ultimate Guide to the vi and ex Text Editors. ENV
is normally assigned to be .kshrc, to be executed
whenever a shell is spawned. For example: In this example ENV
is directly set to .kshrc
in your home directory. The ~
specifies your HOME
directory (see “Tilde Substitution”
for more details). If your .kshrc
is very long and involved, spawning a new shell can take a while.
The ENV line
displayed in the screen above, although complicated, causes the
.kshrc to not be executed, unless
you are in an interactive shell, and therefore
quickly spawns a new shell. (For a complete explanation of this
command line, the START
command line, and interactive shells, see Chapter 23 “Advanced Concepts and Commands”.) TERM
sets the terminal type for which output should be prepared. (You
should set this to the terminal type you are on.) The export
command puts the values of these parameters in the environment (makes
them global) so that subprocesses have access to them. The stty
command sets terminal characteristics to the default (i.e., sane)
values. You should also set susp
to ^Z—that's CTRL -Z—so
you can get job control.
This is an example of just one .profile.
When you create your own .profile using an
editor, you can set many different shell variables depending on
how you want the environment set up. See Table 16-1 “Shell Parameters” for a listing of possible variables. Setting up ENV |  |
This shell script sets values, such as path names and aliases.
These values can then be accessed by shell subprocesses. A ENV
file may look like: lg=/abbreviation/of/long/path/name alias who='who | sort ' set -o monitor trap "$HOME/.logout" 0 |
The first line of the example ENV
file sets an abbreviation for a long path name of a directory. The
variable lg contains
the long path name, and executing: moves you to the /abbreviation/of/long/path/name
directory. Note the dollar sign ($) placed before a parameter, in
this case $lg,
designates using the value of that parameter. The alias
command is explained in Chapter 18 “Aliasing: Abbreviating Commands” and the set
command is explained in Chapter 23 “Advanced Concepts and Commands”. The trap
command line is explained in “Terminating the Shell”. (The above example just gives you an idea
of the types of things to put in an ENV
file.) You can create your own ENV
file using an editor, but you must set the ENV
variable to the name of that file in .profile
or the system does not read it when invoking a new shell. For example,
if you use .kshrc as your
ENV file, the line in your .profile
file might look like: The set Command |  |
There is a command that displays current environment variables,
the set command.
If you type set,
a listing similar to this is displayed: $ set EDITOR=/usr/bin/vi ENV='${START[ (_$- = 1) + (_ = 0) - (_$- != _${-%%*i*}) ]}' START=~/.kshrc FCEDIT=/bin/ed OLDPWD=/usr/bin HOME=/users/mary IFS= HISTFILE=.sh_history HISTSIZE=30 LOGNAME=mary MAIL=/usr/mail/mary MAILCHECK=600 PATH=/bin/posix:/bin:/usr/lib:/usr/bin:/users/mary/bin:. PPID=29590 PS1=$ PS2=> PS3=#? PWD=/users/mary/ksht/man RANDOM=15314 SECONDS=0 SHELL=/bin/ksh START[0]=/users/mary/.kshrc TERM=hp2392 TMOUT=0 TZ=MST7MDT VISUAL=vi |
For an explanation of each of these, see Table 16-1 “Shell Parameters”, and Chapter 23 “Advanced Concepts and Commands”. At this time it is not important that
you understand each of these shell variables completely. These definitions
will become clearer as you become familiar with the POSIX and Korn
Shell. Table 16-1 Shell Parameters Parameter | Definition |
|---|
# | Represents the number, in decimal, of
positional parameters supplied to a shell script. | -
(dash) | Represents the flags or options supplied
to the shell, on invocation, or by other commands. | ? | Represents the decimal value (exit value)
returned by the last executed command. | $ | Represents the process number of the
last invoked shell. Note that it is not reset
for parenthesis subshells. | ! | Represents the process number of the
last background process invoked. | -
(underscore) | Represents the last argument of the previous
command line (for Korn Shell only). | CDPATH | The search path for the cd
command. | COLUMNS | This parameter, when set, defines the
width of the edit window for the shell edit modes (vi,
emacs, gmacs)
and for printing lists from the select
command. | EDITOR | When the VISUAL
parameter is not set and the value of this parameter ends in emacs,
gmacs, or vi,
then the corresponding set -o
option is turned on. (See the set
command in Chapter 23 “Advanced Concepts and Commands”.) | ENV | If this parameter is set to a script's
name, when a shell is invoked the script is executed by the new
shell prior to going interactive. | FCEDIT | Specifies the name of the editor to use
when the fc command
is executed and the fc
command does not designate an editor. | IFS | Internal Field Separators (usually space,
tab, and newline), which are used to separate command words during
command or parameter substitution and when using the read
command. | HISTFILE | This is set to the path name of the file
to be used to store the command history. The default is .sh_history. | HISTSIZE | This is set to the number of saved commands
accessible by the shell. The default size is 128. | HOME | The default for the cd
command, which is your home directory. | LINES | When this is set to a value, that value
determines the column length for printing lists created by the select
command. | MAIL | If this parameter is set to the name
of a mail file and the MAILPATH
parameter is not set, then the shell tells you mail has arrived
in the named file. | MAILCHECK | This parameter specifies how often (in
seconds) the shell checks for the arrival of new mail. The default
is 600 seconds. | MAILPATH | The colon (:)
separated search path for mail files. The shell informs you of mail
arriving in any file in the list within the time specified by MAILCHECK.
If you follow each mail file in the search path with a question
mark (?), the message immediately
following the ?
appears on the screen instead of the default message. | PATH | The search path for commands. | PPID | The process number of the parent of the
current shell. If you execute ps -f,
you will see this number under the PPID
heading. Associated with this PPID
is a PID, which
is the current process number. | PS1 | Defines the primary prompt string for
a shell. The default is $.
If you precede the $
with the ! character,
the primary prompt string includes the number of the current command. | PS2 | Secondary prompt string, by default "> ",
used on command or script continuation lines. | PS3 | The prompt string used with the select
command, by default "#?". | PWD | The present working directory set by
the last cd command. | OLDPWD | The previous working directory set by
the last cd command. | RANDOM | This parameter generates a random integer
when referenced. | REPLY | This parameter is set by the select
and read commands
when no arguments are supplied on the select
command line. Instead, the PS3
prompt is printed and the lines read from standard input are placed
in REPLY. | SECONDS | Returns the number of seconds since the
shell was invoked. | SHELL | The path name where the shell itself
lives. This refers to the user's preferred shell. | TMOUT | If this parameter is set to a value greater
than zero and you do not enter another command or Return
within that number of seconds, the shell terminates. | VISUAL | When this variable is set and ends in
emacs, gmacs,
or vi, then the
corresponding set -o
option is turned on. (See the set
command in Chapter 23 “Advanced Concepts and Commands”.) |
The following variables are set automatically at login: # $ - ? HOME OLDPWD PPID PWD RANDOM REPLY SECONDS SHELL These variables are given default values in the default login
script: IFS MAILCHECK PATH PS1 PS2 PS3 TMOUT Again, use the set
command to check these values before editing or creating a .profile
that changes them.
|