?? _chapter 8.htm
字號:
<html>
<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Chapter 8</title>
<link rel="stylesheet" type="text/css" href="docsafari.css">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body><table width="100%" border="1" bgcolor="#EBEBFF"><tr><td width="5%" align="left" valign="middle"><a href="_chapter 7.htm"><img src="Larrow.gif" width="17" height="19" border="0"></a></td><td align="center" valign="middle"><a class="docLink" href="Front matter.htm">CONTENTS</a></td><td width="5%" align="right" valign="middle"><a href="_chapter 9.htm"><img src="Rarrow.gif" width="17" height="19" border="0"></a></td></tr></table>
<h2 class="docChapterTitle">Chapter 8. The Interactive Bourne Shell</h2><ul><li> <a class="docLink" href="#ch08lev1sec1">8.1 Startup</a></li>
<li> <a class="docLink" href="#ch08lev1sec2">8.2 Programming with the Bourne Shell</a></li>
<li> <a class="docLink" href="#ch08lev1sec3">BOURNE SHELL LAB EXERCISES</a></li>
</ul>
<p class="docText">
<img alt="graphics/ch08.gif" src="ch08.gif" border="0" width="500" height="1152"></p>
<h3 class="docSection1Title" id="ch08lev1sec1">8.1 Startup</h3>
<p class="docText">If the Bourne shell is your login shell, it follows a chain
of processes before you see a shell prompt.</p>
<center>
<h5 id="ch08fig01" class="docFigureTitle">Figure 8.1. Starting the Bourne shell.</h5>
<p class="docText">
<img alt="graphics/08fig01.gif" src="08fig01.gif" border="0" width="375" height="452"></p>
</center>
<p class="docText">The first process to run is called <span class="docEmphasis">
init,</span> PID 1. It gets instructions from a file called
<span class="docEmphasis">inittab</span> (System V), or it spawns a
<span class="docEmphasis">getty</span> process (BSD). These processes open up
the terminal ports, providing a place where standard input comes from and a
place where standard output and error go, and they put a login prompt on your
screen. The <span class="docEmphasis">/bin/login</span> program is then
executed. The <span class="docEmphasis">login</span> program prompts for a
password, encrypts and verifies the password, sets up an initial environment,
and starts up the login shell, <span class="docEmphasis">/bin/sh,</span> the
last entry in the <span class="docEmphasis">passwd</span> file. The
<span class="docEmphasis">sh</span> process looks for the system file,
<span class="docEmphasis">/etc/profile,</span> and executes its commands. It
then looks in the user's home directory for an initialization file called
<span class="docEmphasis">.profile.</span> After executing commands from
<span class="docEmphasis">.profile,</span> the default dollar sign ($) prompt
appears on your screen and the Bourne shell awaits commands.</p>
<h4 class="docSection2Title" id="ch08lev2sec1">8.1.1 The Environment</h4>
<p class="docText">The environment of a process consists of variables, open
files, the current working directory, functions, resource limits, signals, and
so forth. It defines those features that are inherited from one shell to the
next and the configuration for the working environment. The configuration for
the user's shell is defined in the shell initialization files.</p>
<p class="docText"><b>The Initialization Files.</b> After the Bourne shell
program starts up, it first checks for the system file <span class="docEmphasis">
/etc/profile.</span> After executing the commands in that file, the
initialization file, <span class="docEmphasis">.profile,</span> in the user's
home directory, is executed. Skeleton files for initial setup can be found in
<span class="docEmphasis">/etc/skel</span> (SVR4).</p>
<p class="docText"><span class="docEmphStrong">The <span class="docEmphasis">
/etc/profile</span> File.</span> The <span class="docEmphasis">/etc/profile</span>
file is a systemwide initialization file set up by the system administrator to
perform tasks when the user logs on. It is executed when the Bourne 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 <span class="docEmphasis">/etc/motd</span>
file. (The following examples will make more sense after you have completed this
chapter.)</p>
<h5 id="ch08list01" class="docExampleTitle">Example 8.1 </h5>
<pre>(Sample <span class="docEmphasis">/etc/profile</span>)
# <span class="docEmphasis">The profile that all logins get before using their own .profile</span>
1 trap " " 2 3
2 export LOGNAME PATH
3 if [ "$TERM" = " " ]
then
if /bin/i386
then
TERM=AT386 <span class="docEmphasis"># Sets the terminal</span>
else
TERM=sun
fi
export TERM
fi
# <span class="docEmphasis">Login and -su shells get /etc/profile services.</span>
# <span class="docEmphasis">-rsh is given its environment in its own .profile.</span>
4 case "$0" in
-sh | -ksh | -jsh )
5 if [ ! -f .hushlogin ]
then
/usr/sbin/quota
# <span class="docEmphasis">Allow the user to break the Message-Of-The-</span>
# <span class="docEmphasis">Day only.</span>
6 trap "trap ' ' 2" 2
7 /bin/cat -s /etc/motd
<span class="docEmphasis"># Message of the day displayed</span>
trap " " 2
8 /bin/mail -E <span class="docEmphasis"># Checks for new mail</span>
9 case $? in
0)
echo "You have new mail. "
; ;
2)
echo "You have mail. "
;;
esac
fi
esac
10 umask 022
11 trap 2 3
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<span style="FONT-WEIGHT: bold">
<ol class="docList" type="1">
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">trap</span> command
controls signals coming into this program while it is running. If signals
<span class="docEmphasis">2</span> (Control-C) or
<span class="docEmphasis">3</span> (Control-\) are sent while the program
is in execution, those signals will be ignored.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The variables <span class="docEmphasis">LOGNAME</span>
and <span class="docEmphasis">PATH</span> are exported so that their
values will be known in subshells started from this process.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The command <span class="docEmphasis">/bin/i386</span>
is executed. If the exit status of the command is zero, the terminal
variable, <span class="docEmphasis">TERM,</span> is assigned the value
<span class="docEmphasis">AT386;</span> if not, the
<span class="docEmphasis">TERM</span> variable is assigned
<span class="docEmphasis">sun.</span></span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">If the value of <span class="docEmphasis">$0,</span>
the name of the program running the <span class="docEmphasis">/etc/profile</span>
file, is either a login or Bourne, Korn, or job shell, the following
commands will be executed.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">If the <span class="docEmphasis">.hushlogin</span> file
does not exist, <span class="docEmphasis">quota</span> will be run to
display the disk usage warnings if usage is over the quota.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">trap</span> is reset so
that the user can terminate the message of the day with Control-C.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">After the message of the day has been displayed, the
<span class="docEmphasis">trap</span> is reset to ignore Control-C.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">mail</span> program
checks for new incoming mail.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">If the exit status (<span class="docEmphasis">$?</span>)
of the <span class="docEmphasis">mail</span> program is
<span class="docEmphasis">0</span> or <span class="docEmphasis">2,</span>
the message "<span class="docEmphasis">You have new mail.</span>" or "<span class="docEmphasis">You
have mail.</span>", respectively, is displayed.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">umask</span> command is
set to determine the initial permissions of files and directories when
they are created.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">trap</span> command sets
signals <span class="docEmphasis">2</span> and <span class="docEmphasis">3</span>
back to their defaults; i.e., to kill the program if either Control-C or
Control-\ arrive.</span></li>
</ol>
</span></td>
</tr>
</table>
<p class="docText"><span class="docEmphStrong">The <span class="docEmphasis">
.profile</span> File.</span> The <span class="docEmphasis">.profile</span> file
is a user-defined initialization file executed once at login and found in your
home directory. It gives you the ability to customize and modify the shell
environment. Environment and terminal settings are normally put here, and if a
window application or database application is to be initiated, it is started
here. The settings in this file will be discussed in detail as the chapter
progresses, but a brief synopsis of each line in the file is explained here.</p>
<h5 id="ch08list02" class="docExampleTitle">Example 8.2 </h5>
<pre>(Sample .<span class="docEmphasis">profile</span>)
1 TERM=vt102
2 HOSTNAME='uname -n'
3 EDITOR=/usr/ucb/vi
4 PATH=/bin:/usr/ucb:/usr/bin:/usr/local:/etc:/bin:/usr/bin:
5 PS1="$HOSTNAME $ > "
6 export TERM HOSTNAME EDITOR PATH PS1
7 stty erase ^h
8 go () { cd $1; PS1='pwd'; PS1='basename $PS1'; }
9 trap '$HOME/.logout' EXIT
10 clear
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<span style="FONT-WEIGHT: bold">
<ol class="docList" type="1">
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">TERM</span> variable is
assigned the value of the terminal type, <span class="docEmphasis">vt102.</span></span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">Because the <span class="docEmphasis">uname 杗</span>
command is enclosed in backquotes, the shell will perform command
substitution, i.e., the output of the command (the name of the host
machine) will be assigned to the variable <span class="docEmphasis">
HOSTNAME.</span></span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">EDITOR</span> variable is
assigned <span class="docEmphasis">/usr/ucb/vi.</span> Programs such as
<span class="docEmphasis">mail</span> will now have this variable
available when defining an editor.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">PATH</span> variable is
assigned the directory entries that the shell searches in order to find a
UNIX program. If, for example, you type <span class="docEmphasis">ls,</span>
the shell will search the <span class="docEmphasis">PATH</span> until it
finds that program in one of the listed directories. If it never finds the
program, the shell will tell you so.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The primary prompt is assigned the value of
<span class="docEmphasis">HOSTNAME,</span> the machine name, and the
<span class="docEmphasis">$</span> and <span class="docEmphasis">></span>
symbols.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">All of the variables listed are exported. They will be
known by child processes started from this shell.</span></li>
<li><span style="FONT-WEIGHT: normal">
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -