?? _chapter 8.htm
字號:
variable, has been changed since you logged on, you can use the
<span class="docEmphasis">dot</span> command to re-execute the
<span class="docEmphasis">.profile</span> without logging out and then logging
back in.</p>
<h5 id="ch08list07" class="docExampleTitle">Example 8.7 </h5>
<pre>$ <span class="docEmphStrong">. .profile</span>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<p class="docText">The <span class="docEmphasis">dot</span> command executes
the initialization file, <span class="docEmphasis">.profile,</span> within
this shell. Local and global variables are redefined within this shell. The
<span class="docEmphasis">dot</span> command makes it unnecessary to log out
and then log back in again.<span id="ENB8-1"><a class="docLink" href="#EN8-1"><sup>[1]</sup></a></span></td>
</tr>
</table>
<h4 class="docSection2Title" id="ch08lev2sec2">8.1.2 The Command Line</h4>
<p class="docText">After logging on, the shell displays its primary prompt, a
dollar sign, by default. The shell is your command interpreter. When the shell
is running interactively, it reads commands from the terminal and breaks the
command line into words. A command line consists of one or more words (tokens),
separated by whitespace (blanks and/or tabs), and terminated with a newline,
which is generated by pressing Enter. The first word is the command and
subsequent words are the command's arguments. The command may be a UNIX
executable program such as <span class="docEmphasis">ls</span> or
<span class="docEmphasis">pwd,</span> a built-in command such as
<span class="docEmphasis">cd</span> or <span class="docEmphasis">test,</span> or
a shell script. The command may contain special characters, called
<span class="docEmphasis">metacharacters,</span> that the shell must interpret
while parsing the command line. If a command line is long and you want to
continue typing on the next line, the backslash character, followed by a newline,
will allow you to continue typing on the next line. The secondary prompt will
appear until the command line is terminated.</p>
<p class="docText"><b>The Exit Status.</b> After a command or program
terminates, it returns an exit status to the parent process. The exit status is
a number between 0 and 255. By convention, when a program exits, if the status
returned is zero, the command was successful in its execution. When the exit
status is nonzero, the command failed in some way. The shell status variable,
<span class="docEmphasis">?,</span> is set to the value of the exit status of
the last command that was executed. Success or failure of a program is
determined by the programmer who wrote the program.</p>
<h5 id="ch08list08" class="docExampleTitle">Example 8.8 </h5>
<pre>1 $ grep "john" /etc/passwd
<span class="docEmphasis">john:MgVyBsZJavd16s:9496:40:John Doe:/home/falcon/john:/bin/sh</span>
2 $ <span class="docEmphStrong">echo $?</span>
<span class="docEmphasis">0</span>
3 $ grep "nicky" /etc/passwd
4 $ <span class="docEmphStrong">echo $?</span>
<span class="docEmphasis">1</span>
5 $ grep "scott" /etc/passsswd
<span class="docEmphasis">grep: /etc/passsswd: No such file or directory</span>
6 $ <span class="docEmphStrong">echo $?</span>
<span class="docEmphasis">2</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 <span class="docEmphasis">grep</span> program
searches for the pattern <span class="docEmphasis">john</span> in the
<span class="docEmphasis">/etc/passwd</span> file and is successful. The
line from <span class="docEmphasis">/etc/passwd</span> is displayed.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">?</span> variable is set
to the exit value of the <span class="docEmphasis">grep</span> command.
Zero indicates success.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">grep</span> program
cannot find user <span class="docEmphasis">nicky</span> in the
<span class="docEmphasis">/etc/passwd</span> file.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">If the <span class="docEmphasis">grep</span> program
cannot find the pattern, it returns an exit status of
<span class="docEmphasis">1.</span></span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">grep</span> fails because
the <span class="docEmphasis">/etc/passsswd</span> file cannot be opened.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">If <span class="docEmphasis">grep</span> cannot find
the file, it returns an exit status of <span class="docEmphasis">2.</span></span></li>
</ol>
</span></td>
</tr>
</table>
<p class="docText"><b>Multiple Commands at the Command Line.</b> A command line
can consist of multiple commands. Each command is separated by a semicolon, and
the command line is terminated with a newline.</p>
<h5 id="ch08list09" class="docExampleTitle">Example 8.9 </h5>
<pre>$ <span class="docEmphStrong">ls; pwd; date</span>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<p class="docText">The commands are executed from left to right, one after
the other, until the newline is reached.</td>
</tr>
</table>
<p class="docText"><b>Grouping Commands.</b> Commands may also be grouped so
that all of the output is either piped to another command or redirected to a
file.</p>
<h5 id="ch08list10" class="docExampleTitle">Example 8.10 </h5>
<pre>$ <span class="docEmphStrong">( ls ; pwd; date ) > outputfile</span>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<p class="docText">The output of each of the commands is sent to the file
called <span class="docEmphasis">outputfile.</span> The spaces inside the
parentheses are necessary.</td>
</tr>
</table>
<p class="docText"><b>Conditional Execution of Commands.</b> With conditional
execution, two command strings are separated by the special metacharacters,
double ampersands (<span class="docEmphasis">&&</span>) and double vertical bars
(||). The command on the right of either of these metacharacters will or will
not be executed based on the exit condition of the command on the left.</p>
<h5 id="ch08list11" class="docExampleTitle">Example 8.11 </h5>
<pre>$ <span class="docEmphStrong">cc prgm1.c 杘 prgm1 && prgm1</span>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<p class="docText">If the first command is successful (has a zero exit
status), the command after the <span class="docEmphasis">&&</span> is
executed; i.e., if the <span class="docEmphasis">cc</span> program can
successfully compile <span class="docEmphasis">prgm1.c,</span> the resulting
executable program, <span class="docEmphasis">prgm1,</span> will be
executed.</td>
</tr>
</table>
<h5 id="ch08list12" class="docExampleTitle">Example 8.12 </h5>
<pre>$ <span class="docEmphStrong">cc prog.c >& err || mail bob < err</span>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<p class="docText">If the first command fails (has a nonzero exit status),
the command after the <span class="docEmphasis">||</span> is executed; i.e.,
if the <span class="docEmphasis">cc</span> program cannot compile
<span class="docEmphasis">prog.c,</span> the errors are sent to a file
called <span class="docEmphasis">err,</span> and user
<span class="docEmphasis">bob</span> will be mailed the
<span class="docEmphasis">err</span> file.</td>
</tr>
</table>
<p class="docText"><b>Commands in the Background.</b> Normally, when you execute
a command, it runs in the foreground, and the prompt does not reappear until the
command has completed execution. It is not always convenient to wait for the
command to complete. By placing an ampersand (<span class="docEmphasis">&</span>)
at the end of the command line, the shell will return the shell prompt
immediately and execute the command in the background concurrently. You do not
have to wait to start up another command. The output from a background job will
be sent to the screen as it processes. Therefore, if you intend to run a command
in the background, the output of that command might be redirected to a file or
piped to another device, such as a printer, so that the output does not
interfere with what you are doing.</p>
<p class="docText">The <span class="docEmphasis">$!</span> variable contains the
PID number of the last job put in the background.</p>
<h5 id="ch08list13" class="docExampleTitle">Example 8.13 </h5>
<pre>1 $ <span class="docEmphStrong">man sh | lp&</span>
2 <span class="docEmphasis">[1] 1557</span>
3 $ <span class="docEmphStrong">kill -9 $!</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">man</span>
command (the manual pages for the <span class="docEmphasis">sh</span>
command) is piped to the printer. The ampersand at the end of the command
line puts the job in the background.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">There are two numbers that appear on the screen: the
number in square brackets indicates that this is the first job to be
placed in the background; the second number is the PID, or the process
identification number of this job.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The shell prompt appears immediately. While your
program is running in the background, the shell is waiting for another
command in the foreground.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">!</span> variable
evaluates to the PID of the job most recently put in the background. If
you get it in time, you will kill this job before it goes to the print
queue.</span></li>
</ol>
</span></td>
</tr>
</table>
<h4 class="docSection2Title" id="ch08lev2sec3">8.1.3 Metacharacters (Wildcards)</h4>
<p class="docText">Metacharacters are special characters used to represent
something other than themselves. Shell metacharacters are called
<span class="docEmphasis">wildcards.</span>
<a class="docLink" href="#ch08table01">Table 8.1</a> lists metacharacters and
what they do.</p>
<table cellSpacing="0" cellPadding="1" width="100%" border="1">
<caption>
<h5 id="ch08table01" class="docTableTitle">Table 8.1. Shell Metacharacters</h5>
</caption>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -