Tải bản đầy đủ (.docx) (7 trang)

Initialization Files

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (127.03 KB, 7 trang )

Initialization Files
Objectives
Upon completion of this module, you should be able to:
• Name the different initialization files for the Bourne Again, Bourne, Korn, and C shells.
• Define the purpose of initialization files
• Describe where the initialization files are located and what types of settings can be
defined
• Set the ENV variable to enable the .kshrc file to be read
Discussion – Certain types of information, such as your default printer, are set each time you log
in. What other types of customizable settings could be set at the beginning of each session?
Features of Initialization Files
Overview
Initialization files contain a series of commands and variable settings that are executed
when a shell is started. These files customize the environment for the system and the user.
There are two levels of initialization files. The first level is system-wide. System
initialization files are maintained by a system administrator and reside in the /etc
directory.
The second level is user-specific initialization files that reside in a user’s home directory.
These files are listed in Table 11-1.
Table 11-1 User-Specific Initialization Files
Shell System-Wide User-Specified
Read First) (Read Second)
Bourne-Again 1. /etc/profile 2. $HOME/.bash_profile
3. $HOME/.bashrc
Bourne 1. /etc/profile 2. $HOME/.profile
Korn 1. /etc/profile 2. $HOME/.profile
ENV=$HOME/.kshrc;export ENV
3. $HOME/.kshrc
C 1. /etc/.login 2. $HOME/.login
3. $HOME/.cshrc
4. $HOME/.logout


Note –$HOME is the user’s home directory.
When a predefined environment variable is placed in one of the system-wide initialization
files, it is recognized globally in any shell or subshell. Users can customize many of these
variables for their own environment by placing them in the initialization files in their
home directory. To make the customizations available to all subsequent shells, the
variable must be exported. Unlike variables, commands cannot be exported. If commands
are used in the initialization files,
they must be placed in the secondary initialization files in order to be read each time a
subshell is opened.
The /etc/profile Script
When a user logs in, the system reads the /etc/profile file first, and then it reads
the user’s ~/.bash_profile file. This means that the user’s preferences for variable
settings can override the default settings that appear in the /etc/profile file.
The /etc/profile file:
• Exports environment variables such as LOGNAME for login name
• Exports PATH for the default command path
• Sets the variable TERM for the default terminal type
• Displays the contents of the /etc/motd file
• Sets the default file creation permissions
• Checks if you have mail and will print a mail message upon login
This file is maintained by the system administrator.
User-Specific Initialization Files
Login Sequence
When you log in to your system, the system and user’s initialization files for the
appropriate shell are read, and the tasks defined in the initialization files are executed.
The initialization files provide great flexibility to the user for customizing their
environment. Generally, these files are set up as templates by the system administrator,
and then modified by the user.
The user-specific initialization file(s) are stored in the home directory of the user. These
initialization files can perform all or part of the following:

• Set the default prompt
• Define the default printer
• Set default permissions
• Set the default terminal type used by vi and other tools
• Tell the shell where to look for new mail
• Set noclobber to prevent overwriting of files during redirection
• Set the command path to the user’s specification
• Set up custom commands
When you first log in to the system, you are placed in the default shell as defined by the
/etc/passwd entry for your account.
The initialization files are read as shown in Table 11-2. The .bash_profile,
.profile,.login, .bashrc,.cshrc,.kshrc files are assumed to be in the
user’s home directory.
Table 11-2 Initialization Files Read Process
Shell Read during login Read opening shell
Bourne-Again /etc/profile, .bash_profile and .bashrc
.bashrc
Bourne /etc/profile and .profile
Korn /etc/profile, .profile and .kshrc
.kshrc
C /etc/.login, .cshrc and .login .cshrc
Shells
The login shell is assigned by the system administrator as part of creating a user account. The
login shell determines which initialization files are read during login.
Bourne Again Shell
The Bourne Again shell uses .bash_profile, .bashrc files to set the user’s
environment.
When you log in to the system, the .bash_profile file is read. Then, the .bashrc
file is read. The .bash_profile file is only read once, while the .bashrc file is
read every time a new bash shell is opened.

Bourne Shell
The Bourne shell uses .profile, a user-specific initialization file to set the user’s
environment. The .profile file is only read once during login.
Korn Shell
The Korn shell uses two user-specific environment files to set the user’s environment:
• .profile
• .kshrc
When you log in to the system, the .profile file is read. Then, if the ENV variable in
the .profile file is assigned and exported, the .kshrc file is read. The .profile
file is only read once, while the .kshrc file is read every time a new Korn shell is
opened.
Many Korn shell commands cannot be executed by the Bourne shell and therefore should
not be placed in the .profile file. These Korn shell specific commands and features
should be placed in the .kshrc file.
The contents of the .kshrc file can include
 A customized prompt
 Custom variables
 Aliases
To have the system reread the .kshrc or .profile file after changes have been
made, the user can either log out and log back in, or type the following from the command
line:
$ . ~/.kshrc
$ . ~/.profile
C Shell
The C shell uses two user-specific environment files to set the user’s environment:
 .login
 .cshrc
Both files are located in the user’s home directory. The .login file is read only when a
user logs in to the system, whereas the .cshrc file is read each time a user invokes the
C shell.

To have the system reread the .login or .cshrc file after changes have been made,
the user can either log out and log back in, or type the following from the command line:
% source ~/.login
% source ~/.cshrc
The ENV Environment Variable
The ENV variable defines the path to the .kshrc file. This variable must be stored in the
.profile file to inform the system that the.kshrc file exists and is to be read when
creating a Korn shell.
Command Format
ENV=$HOME/ filename; export ENV
Setting the .kshrc Variable
This command sets the environment variable to point to the .kshrc file in the $HOME
directory. HOME is a variable that is defined by the system to be the absolute path to the
user’s login directory. Preceding the HOME variable with the dollar sign ($)
metacharacter enables the system to use the value of HOME in the specified location.
$ ENV=$HOME/.kshrc; export ENV
When the ENV variable is placed in the .profile file and read by other shells (such
as the Bourne shell), it does not act as a pointer to the .kshrc file. This variable has no
meaning outside of the Korn shell.
Exercise: Setting Initialization Files
Exercise objective – In this exercise, you will demonstrate an understanding of the concepts
covered in this module.
Tasks
Write the information requested in the space provided.
1. What is an initialization file?
___________________________________________________________
___________________________________________________________
___________________________________________________________
___________________________________________________________
2. Where are the user-specific initialization files stored?

___________________________________________________________
3. Name the user-specific initialization files for the Bourne-Again shell.
___________________________________________________________
___________________________________________________________
4. Describe or name at least three settings that can be stored in the .bashprofile or .login
initialization file for each shell.
___________________________________________________________
___________________________________________________________
___________________________________________________________

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×