?? _chapter 11.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 11</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 10.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 12.htm"><img src="Rarrow.gif" width="17" height="19" border="0"></a></td></tr></table>
<h2 class="docChapterTitle">Chapter 11. The Interactive
<span class="docEmphasis">bash</span> Shell</h2><ul><li> <a class="docLink" href="#ch11lev1sec1">11.1 Introduction</a></li>
<li> <a class="docLink" href="#ch11lev1sec2">11.2 Command Line Shortcuts</a></li>
<li> <a class="docLink" href="#ch11lev1sec3">11.3 Variables</a></li>
<li> <a class="docLink" href="#ch11lev1sec4">BASH SHELL LAB EXERCISES</a></li>
</ul>
<p class="docText">
<img alt="graphics/ch11.gif" src="ch11.gif" border="0" width="500" height="448"></p>
<h3 class="docSection1Title" id="ch11lev1sec1">11.1 Introduction</h3>
<p class="docText">With an interactive shell, the standard input, output, and
error are tied to a terminal. When using the Bourne Again (<span class="docEmphasis">bash</span>)
shell interactively, you type UNIX commands at the <span class="docEmphasis">
bash</span> prompt and wait for a response. <span class="docEmphasis">Bash</span>
provides you with a large assortment of built-in commands and command line
shortcuts, such as history, aliases, file and command completion, command line
editing, and many more. Some of the features were present in the standard UNIX
Bourne shell but the GNU project has expanded the shell to include a number of
new features as well adding POSIX compliancy. With the release of
<span class="docEmphasis">bash</span> 2.x, so many features of the UNIX Korn
shell and C shell have been included that the <span class="docEmphasis">bash</span>
shell is a fully functional shell at both the interactive and programming level,
while upwardly compatible with the standard Bourne shell. For UNIX users,
<span class="docEmphasis">bash</span> offers an alternative to the standard
shells, <span class="docEmphasis">sh,</span> <span class="docEmphasis">csh,</span>
and <span class="docEmphasis">ksh.</span><span id="ENB11-1"><a class="docLink" href="#EN11-1"><sup>[1]</sup></a></span></p>
<p class="docText">This chapter focuses on how you interact with
<span class="docEmphasis">bash</span> at the command line and how to customize
your working environment. You will learn how to take advantage of all shortcuts
and built-in features in order to create an efficient and fun working
environment. The next chapter takes you a step further. Then you will be ready
to write <span class="docEmphasis">bash</span> shell scripts to further tailor
the working environment for yourself by automating everyday tasks and developing
sophisticated scripts, and if you are an administrator, doing the same not only
for yourself but also for whole groups of users.</p>
<h4 class="docSection2Title" id="ch11lev2sec1">11.1.1 Versions of <span class="docEmphasis">bash</span></h4>
<p class="docText">The Bourne Again shell is a Capricorn, born on January 10,
1988, fathered by Brian Fox and later adopted by Chet Ramey, who now officially
maintains <span class="docEmphasis">bash,</span> enhances it, and fixes bugs.
The first version of <span class="docEmphasis">bash</span> was 0.99. The current
version (as of this writing) is version 2.04. Major enhancements were completed
in version 2.0, but there are a num ber of operating systems that are still
using version 1.14.7. All versions are freely available under the GNU public
license. To see what version you are using, use the <span class="docEmphasis">-
-version</span> option to <span class="docEmphasis">bash</span> or print the
value of the <span class="docEmphasis">BASH_VERSION</span> environment variable.</p>
<h5 id="ch11list01" class="docExampleTitle">Example 11.1 </h5>
<pre>$ <span class="docEmphStrong">bash --version</span>
<span class="docEmphasis">GNU bash, version 2.03.0(1)-release (sparc-sun-solaris)</span>
<span class="docEmphasis">Copyright 1998 Free Software Foundation, Inc.</span>
$ <span class="docEmphStrong">echo $BASH_VERSION</span>
<span class="docEmphasis">2.03.0(1)-release</span>
</pre>
<h4 class="docSection2Title" id="ch11lev2sec2">11.1.2 Startup</h4>
<p class="docText">If the <span class="docEmphasis">bash</span> shell is your
login shell, it follows a chain of processes before you see a shell prompt.<span id="ENB11-2"><a class="docLink" href="#EN11-2"><sup>[2]</sup></a></span></p>
<center>
<h5 id="ch11fig01" class="docFigureTitle">Figure 11.1. Starting the <span class="docEmphasis">
bash</span> shell.</h5>
<p class="docText">
<img alt="graphics/11fig01.gif" src="11fig01.gif" border="0" width="500" height="602"></p>
</center>
<p class="docText">When the system boots, the first process to run is called
<span class="docEmphasis">init,</span> PID #1. It spawns a
<span class="docEmphasis">getty</span> process. This process opens up the
terminal ports, providing a place where standard input comes from and a place
where standard output and errors go, and puts 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/bash,</span> the last entry in the
<span class="docEmphasis">passwd</span> file. The <span class="docEmphasis">bash</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">.bash_profile.</span> After
executing commands from <span class="docEmphasis">bash _profile,</span>
<span id="ENB11-3"><a class="docLink" href="#EN11-3"><sup>[3]</sup></a></span> it
will execute a command from the user's <span class="docEmphasis">ENV</span>
file, usually called <span class="docEmphasis">.bashrc,</span> and finally the
default dollar sign (<span class="docEmphasis">$</span>) prompt appears on your
screen and the shell waits for commands. (For more on initialization files, see
"<a class="docLink" href="#ch11lev2sec3">The Environment</a>".)</p>
<p class="docText"><b>Changing the Shell at the Command Line.</b> If you want to
start another shell from the command line temporarily (without changing the
<span class="docEmphasis">/etc/passwd</span> file), just type the name of the
shell. For example, if you are currently using the standard Bourne shell and
would rather have <span class="docEmphasis">bash</span> as your shell, you can
change the shell at the command line simply by typing <span class="docEmphasis">
bash.</span></p>
<h5 id="ch11list02" class="docExampleTitle">Example 11.2 </h5>
<pre>1 $ <span class="docEmphStrong">ps</span>
<span class="docEmphasis">PID TTY TIME CMD</span>
<span class="docEmphasis">1574 pts/6 0:00 sh</span>
2 $ <span class="docEmphStrong">bash</span>
<span class="docEmphasis">bash-2.03$</span>
3 bash-2.03$ <span class="docEmphStrong">ps</span>
<span class="docEmphasis">PID TTY TIME CMD</span>
<span class="docEmphasis">1574 pts/6 0:00 sh</span>
<span class="docEmphasis">1576 pts/6 0:00 bash</span>
</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 output of the <span class="docEmphasis">ps</span>
command shows what processes are running. Currently,
<span class="docEmphasis">sh</span> (the Bourne shell) is running.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">At the Bourne shell prompt, the user enters
<span class="docEmphasis">bash</span> and starts up the Bourne Again
shell. A new prompt appears.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">At the bash prompt, the <span class="docEmphasis">ps</span>
command is executed. The output shows that two shells are running and the
current shell is now <span class="docEmphasis">bash.</span></span></li>
</ol>
</span></td>
</tr>
</table>
<h4 class="docSection2Title" id="ch11lev2sec3">11.1.3 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> The
<span class="docEmphasis">bash</span> shell has a number of startup files that
are sourced. Sourcing a file causes all settings in the file to become part of
the current shell; i.e., a subshell is not created. (The source command is
discussed in "The source or dot Command" on page 621.) The initialization files
are sourced depending on the whether the shell is a login shell, an interactive
shell (but not the login shell), or a noninteractive shell (a shell script).</p>
<p class="docText">When you log on, before the shell prompt appears, the
systemwide initialization file, <span class="docEmphasis">etc/profile,</span> is
sourced. Next, if it exists, the <span class="docEmphasis">.bash_profile</span>
in the user's home directory is sourced. It sets the user's aliases and
functions and then sets user-specific environment variables and startup scripts.</p>
<p class="docText">If the user doesn't have a <span class="docEmphasis">.bash_profile,</span>
but does have a file called <span class="docEmphasis">.bash_login,</span> that
file will be sourced, and if he doesn't have a <span class="docEmphasis">.bash_login,</span>
but does have a <span class="docEmphasis">.profile,</span> it will be sourced.
(The <span class="docEmphasis">.bash_login</span> file is similar to the C
shell's <span class="docEmphasis">.login</span> file and the
<span class="docEmphasis">.profile</span> is normally sourced by the Bourne
shell when it starts up.)</p>
<p class="docText">Here is a summary of the order that <span class="docEmphasis">
bash</span> processes its initialization files <span id="ENB11-4"><a class="docLink" href="#EN11-4"><sup>[4]</sup></a></span> (see
<a class="docLink" href="#ch11fig02">figure 11.2</a>):</p>
<center>
<h5 id="ch11fig02" class="docFigureTitle">Figure 11.2. Order of processing initialization
files.</h5>
<p class="docText">
<img alt="graphics/11fig02.gif" src="11fig02.gif" border="0" width="500" height="186"></p>
</center>
<pre>if <span class="docEmphasis">/etc/profile</span> exists, source it,
if <span class="docEmphasis">~/.bash_profile</span> exists, source it,
if <span class="docEmphasis">~/.bashrc</span> exists, source it,
else if <span class="docEmphasis">~/.bash_login</span> exists, source it,
else if <span class="docEmphasis">~/.profile</span> exists, source it.
</pre>
<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
<span class="docEmphasis">bash</span> shell starts up. It is also 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="ch11list03" class="docExampleTitle">Example 11.3 </h5>
<pre>(Sample <span class="docEmphasis">/etc/profile</span>)
<span class="docEmphasis"># /etc/profile</span>
<span class="docEmphasis"># Systemwide environment and startup programs</span>
<span class="docEmphasis"># Functions and aliases go in /etc/bashrc</span>
1 PATH="$PATH:/usr/X11R6/bin"
2 PS1="[\u@\h \W]\\$ "
3 ulimit -c 1000000
4 if [ 'id -gn' = 'id -un' -a 'id -u' -gt 14 ]; then
5 umask 002
else
umask 022
fi
6 USER='id -un'
7 LOGNAME=$USER
8 MAIL="/var/spool/mail/$USER"
9 HOSTNAME='/bin/hostname'
10 HISTSIZE=1000
11 HISTFILESIZE=1000
12 export PATH PS1 HOSTNAME HISTSIZE HISTFILESIZE USER LOGNAME MAIL
13 for i in /etc/profile.d/*.sh ; do
14 if [ -x $i ]; then
15 . $i
fi
16 done
17 unset i #
</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">PATH</span> variable is
assigned locations where the shell should search for commands.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The primary prompt is assigned. It will be displayed in
the shell window as the user's name (<span class="docEmphasis">\u</span>),the
@ symbol, the host machine (<span class="docEmphasis">\W</span>), and a
dollar sign.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">ulimit</span> command (a
shell built-in command) is set to limit the maximum size of core files
created to 1,000,000 bytes. Core files are memory dumps of programs that
have crashed, and they take up a lot of disk space.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">This line reads, <span class="docEmphasis">if the
user's group name is equal to the user's name and the user's ID number is
greater than 14
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -