Tải bản đầy đủ (.pdf) (63 trang)

Sun Fundamentals of Solaris 7 EU-118 Student Guide With Instructor Notes phần 8 pdf

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 (349.48 KB, 63 trang )

10
10-20 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Exercise: Using Bourne and Korn Shell Variables
Exercise Solutions
6. Determine how your current PATH variable is set.
$ echo $PATH
7. Add the /usr/ucb directory to your path and export the variable.
$ PATH=$PATH:/usr/ucb; export PATH
10
Bourne and Korn Shell Variables 10-21
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Check Your Progress
Before continuing on to the next module, check that you are able to
accomplish or answer the following:
❑ Set and unset shell and environment variables for the Bourne and
Korn shells
❑ Change the PATH environment variable for the Bourne and Korn
shells
❑ Use the which command to determine which version of a
command is being used
❑ Use the whereis command to search for instances of a command
on a system
10
10-22 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Think Beyond
Setting variables at the command line every time you log in can be
tedious. How could you make these variable definitions a permanent
part of your environment? How might a system administrator define
variables for all users on a system?


11-1
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
InitializationFiles
11
Objectives
Upon completion of this module, you should be able to:
● Name the different initialization files for the Bourne, Korn, and C
shells
● Identify the initialization file used by the Common Desktop
Environment
● 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
11
11-2 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Relevance

Present the following question to stimulate the students and get them thinking about the
issues and topics presented in this module. While they are not expected to know the
answer to the question, the answer should be of interest to them and inspire them to learn
the content presented in this module.
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?
Additional Resources
Additional resources – The following references can provide
additional details on the topics discussed in this module:
● Solaris User’s Guide, Part Number 802-6499

● Solaris Advanced User’s Guide to System and Network Tasks,
Chapters 1 and 3, Part Number 801-4415-10
11
Initialization Files 11-3
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
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.
11
11-4 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Features of Initialization Files
Overview
The second level is user-specific initialization files that reside in a
user’s home directory. These files are listed in Table 11-1.
Note – ENV is a variable that is set in the .profile file to direct the
system to read an alternate initialization file. $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.
Table 11-1 User-Specific Initialization Files
Shell
System-Wide
(Read First)
User-Specific
(Read Second)
Bourne 1. /etc/profile 2. $HOME/.profile
Korn 1. /etc/profile 2. $HOME/.profile
ENV=$HOME/.kshrc;export ENV
3. $HOME/.kshrc
C1./etc/.login 2. $HOME/.login
3. $HOME/.cshrc
4. $HOME/.logout
11
Initialization Files 11-5
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Features of Initialization Files
The /etc/profile Script
When a Bourne or Korn shell user logs in, the system reads the
/etc/profile file first, and then it reads the user’s .profile file,
and, for Korn shell users, the .kshrc 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.
11
11-6 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
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.
11
Initialization Files 11-7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
User-Specific Initialization Files
Login Sequence
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

11
11-8 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
User-Specific Initialization Files
Login Sequence
Figure 11-1 Solaris Login Sequence
$HOME/.profile
$HOME/.kshrc
$HOME/.kshrc
ksh
ksh
ksh
/etc/profile
Open subshell
Log in
Log out
exit
Open subshell
exit
exit
$HOME/.kshrc
11
Initialization Files 11-9
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
User-Specific Initialization Files
Login Sequence
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.
As described in Module 10, ‘‘Bourne and Korn Shell Variables,” if a
new subshell is created by entering the command sh (Bourne shell),

ksh (Korn shell), or csh (C shell), a subshell is created. This process
can be repeated to create additional subshells. To change to the
previous shell, type exit or press Control-d.
The initialization files are read as shown in Table 11-2. The .profile,
.kshrc, .login, and .cshrc 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 /etc/profile and .profile
Korn /etc/profile, .profile, and
.kshrc
.kshrc
C /etc/.login, .cshrc, and .login .cshrc
11
11-10 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
User-Specific Initialization Files
The .dtprofile
For CDE users there is another initialization file called .dtprofile
which resides in the user’s home directory and determines settings for
CDE. This file is generally set up to be standard system wide and is
created in a user’s home directory the first time the user logs in to
CDE. In versions of CDE shipped with Solaris 2.6 and later, the
.dtprofile causes the system to read a Korn shell user’s .kshrc file
each time a terminal window is opened within CDE, and the
.profile and .kshrc files each time a console window is opened.
Both initialization files are also read when CDE initially starts up.
For versions of CDE shipped with versions of Solaris earlier than 2.6, it
was necessary to make changes to the .profile or .login files as
recommended within the standard .dtprofile, and then uncomment

(remove the # sign from in front of) the line reading
DTSOURCEPROFILE=true.

If
DTSOURCEPROFILE
line is commented out, none of the initialization files are read (with the
exception of
.dtprofile
).
11
Initialization Files 11-11
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
User-Specific Initialization Files
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 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.
11
11-12 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
User-Specific Initialization Files
Shells
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, discussed in detail in Module 12,
‘‘Basic Features of the Korn Shell,” 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
11
Initialization Files 11-13
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
User-Specific Initialization Files
Shells
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
11
11-14 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
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
11
Initialization Files 11-15
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
The ENV Environment Variable
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.
11
11-16 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
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 Korn and C shells.
___________________________________________________________
___________________________________________________________
4. What is the initialization file for CDE called?
___________________________________________________________
5. Describe or name at least three settings that can be stored in the
.profile or .login initialization file for each shell.
___________________________________________________________
___________________________________________________________
___________________________________________________________
6. Describe or name at least two settings that can be stored in the
.kshrc or .cshrc file.
___________________________________________________________

___________________________________________________________
11
Initialization Files 11-17
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Exercise: Setting Initialization Files
Exercise Summary
Discussion – Take a few minutes to discuss what experiences, issues,
or discoveries you had during the lab exercises.

Manage the discussion here based on the time allowed for this module, which was given
in the “About This Course” module. If you find you do not have time to spend on
discussion, then just highlight the key concepts students should have learned from the
lab exercise.
● Experiences

Ask students what their overall experiences with this exercise have been. You might want
to go over any trouble spots or especially confusing areas at this time.
● Interpretations

Ask students to interpret what they observed during any aspects of this exercise.
● Conclusions

Have students articulate any conclusions they reached as a result of this exercise
experience.
● Applications

Explore with students how they might apply what they learned in this exercise to
situations at their workplace.
11
11-18 Fundamentals of Solaris 7

Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Exercise: Setting Initialization Files
Exercise Solutions
Write the information requested in the space provided.
1. What is an initialization file?
An initialization file is an environment file that generally resides in the
user’s home directory and is used to customize the user’s environment.
2. Where are the user-specific initialization files stored?
User-specific initialization files are stored in the user’s home directory.
3. Name the user-specific initialization files for the Korn and C shells.
.profile and .kshrc (Korn shell) and .login and .cshrc (C shell)
4. What is the initialization file for CDE called?
.dtprofile
5. Describe or name at least three settings that can be stored in the
.profile or .login initialization file for each shell.
Time zone, terminal type, PATH, user’s login name, default printer, local
language, and where shell should look for new mail and environment
variables.
6. Describe or name at least two settings that can be stored in the
.kshrc or .cshrc file.
Aliases, history, and a customized prompt.
11
Initialization Files 11-19
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Check Your Progress
Before continuing on to the next module, check that you are able to
accomplish or answer the following:
❑ Name the different initialization files for the Bourne, Korn, and C
shells
❑ Identify the initialization file used by the Common Desktop

Environment
❑ 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
11
11-20 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Think Beyond
What kinds of settings and customizations would you put into your
initialization files at work?
12-1
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
BasicFeaturesof theKorn Shell
12
Objectives
Upon completion of this module, you should be able to:
● Use basic shell quoting
● Customize the Korn shell
● Customize commands in the Korn shell using the alias command
● Review previously executed commands in the Korn shell using the
history command
● Set the command-line editor and edit previously issued
commands
12
12-2 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Relevance

Present the following question to stimulate the students and get them thinking about the

issues and topics presented in this module. While they are not expected to know the
answer to the question, the answer should be of interest to them and inspire them to learn
the content presented in this module.
Discussion – What customizations do you perform repetitively that
you would like to set automatically?
Additional Resources
Additional resources – The following references can provide
additional details on the topics discussed in this module:
● Solaris User’s Guide, Part Number 802-6499
● Solaris Advanced User’s Guide to System and Network Tasks,
Chapters 1 and 3, Part Number 801-4415-10

×