⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 _chapter 9.htm

📁 linux、unix初学者的必读书籍 详细讲述了shell编程方法与技巧
💻 HTM
📖 第 1 页 / 共 5 页
字号:
   <span class="docEmphasis">/home/jody/ellie</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 each of the commands is sent to the file 
      called <span class="docEmphasis">outputfile.</span> Without the 
      parentheses, the output of the first two commands would go to the screen, 
      and only the output of the <span class="docEmphasis">cal</span> command 
      would be redirected to the output file.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The <span class="docEmphasis">pwd</span> command 
      displays the present working directory. The parentheses cause the commands 
      enclosed within them to be processed by a subshell. The
      <span class="docEmphasis">cd</span> command is built into the shell. While 
      in the subshell, the directory is changed to <span class="docEmphasis">
      root</span> and the present working directory is displayed. When out of 
      the subshell, the present working directory of the original shell is 
      displayed.</span></li>
    </ol>
    </span></td>
  </tr>
</table>

<p class="docText"><b>Conditional Execution of Commands.</b> With conditional 
execution, two command strings are separated by two special metacharacters, 
double ampersand and double vertical (<span class="docEmphasis">&amp;&amp;</span> and
<span class="docEmphasis">||</span>). 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="ch09list08" class="docExampleTitle">Example 9.8 </h5>
<pre>% <span class="docEmphStrong">grep '^tom:' /etc/passwd &amp;&amp; mail tom &lt; letter</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 second command after the <span class="docEmphasis">&amp;&amp;</span> is 
    executed. If the <span class="docEmphasis">grep</span> command successfully 
    finds <span class="docEmphasis">tom</span> in the <span class="docEmphasis">
    passwd</span> file, the command on the right will be executed: The
    <span class="docEmphasis">mail</span> program will send
    <span class="docEmphasis">tom</span> the contents of the
    <span class="docEmphasis">letter</span> file.</td>
  </tr>
</table>

<h5 id="ch09list09" class="docExampleTitle">Example 9.9 </h5>
<pre>% <span class="docEmphStrong">grep '^tom:' /etc/passwd || echo &quot;tom is not a user here.&quot;</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 second command after the <span class="docEmphasis">||</span> is 
    executed. If the <span class="docEmphasis">grep</span> command does not find
    <span class="docEmphasis">tom</span> in the <span class="docEmphasis">passwd</span> 
    file, the command on the right will be executed: The
    <span class="docEmphasis">echo</span> program will print
    <span class="docEmphasis">tom is not a user here.</span> to the screen.</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 at the end of the command line, the 
shell will return the shell prompt immediately so that you do not have to wait 
for the last command to complete before starting another one. The command 
running in the background is called a <span class="docEmphasis">background job</span> 
and its output will be sent to the screen as it processes. It can be confusing 
if two commands are sending output to the screen concurrently. To avoid 
confusion, you can send the output of the job running in the background to a 
file or pipe it to another device such as a printer. It is often handy to start 
a new shell window in the background, so you will have access to both the window 
from which you started and the new shell window.</p>
<h5 id="ch09list10" class="docExampleTitle">Example 9.10 </h5>
<pre>1   % <span class="docEmphStrong">man xview | lp&amp;</span>
2   <span class="docEmphasis">[1] 1557</span>
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 output from the man pages for the
      <span class="docEmphasis">xview</span> program 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, and the second number is the PID 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 prompting you for 
      another command in the foreground.</span></li>
    </ol>
    </span></td>
  </tr>
</table>

<h4 class="docSection2Title" id="ch09lev2sec4">9.1.4 Command Line History</h4>
<p class="docText">The history mechanism is built into the C shell. It keeps a 
numbered list of the commands (called <span class="docEmphasis">history events</span>) 
that you have typed at the command line. You can recall a command from the 
history list and reexecute it without retyping the command. The history 
substitution character, the exclamation point, is often called the
<span class="docEmphasis">bang</span> character. The history built-in command 
displays the history list.</p>
<h5 id="ch09list11" class="docExampleTitle">Example 9.11 </h5>
<pre>(The Command Line)
% <span class="docEmphStrong">history</span>
<span class="docEmphasis">1 cd</span>
<span class="docEmphasis">2 ls</span>
<span class="docEmphasis">3 more /etc/fstab</span>
<span class="docEmphasis">4 /etc/mount</span>
<span class="docEmphasis">5 sort index</span>
<span class="docEmphasis">6 vi index</span>
</pre>

<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText">The history list displays the last commands that were 
    typed at the command line. Each event in the list is preceded with a number.</td>
  </tr>
</table>

<p class="docText"><b>Setting History.</b> The C shell <span class="docEmphasis">
history</span> variable is set to the number of events you want to save from the 
history list and display on the screen. Normally, this is set in the
<span class="docEmphasis">cshrc</span> file, the user's initialization file.</p>
<h5 id="ch09list12" class="docExampleTitle">Example 9.12 </h5>
<pre><span class="docEmphStrong">set history=50</span>
</pre>

<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText">The last <span class="docEmphasis">50</span> commands 
    typed at the terminal are saved and may be displayed on the screen by typing 
    the <span class="docEmphasis">history</span> command.</td>
  </tr>
</table>

<p class="docText"><b>Saving History.</b> To save history events across logins, 
set the <span class="docEmphasis">savehist</span> variable. This variable is 
normally set in the <span class="docEmphasis">.cshrc</span> file, the user's 
initialization file.</p>
<h5 id="ch09list13" class="docExampleTitle">Example 9.13 </h5>
<pre><span class="docEmphStrong">set savehist=25</span>
</pre>

<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText">The last <span class="docEmphasis">25</span> commands 
    from the history list are saved and will be at the top of the history list 
    the next time you log in.</td>
  </tr>
</table>

<p class="docText"><b>Displaying History.</b> The <span class="docEmphasis">
history</span> command displays the events in the history list. The history 
command also has options that control the number of events and the format of the 
events that will be displayed. The numbering of events does not necessarily 
start at one. If you have 100 commands on the history list, and you have set the 
history variable to 25, you will only see the last 25 commands saved.</p>
<h5 id="ch09list14" class="docExampleTitle">Example 9.14 </h5>
<pre>% <span class="docEmphStrong">history</span>
<span class="docEmphasis">1 ls</span>
<span class="docEmphasis">2 vi file1</span>
<span class="docEmphasis">3 df</span>
<span class="docEmphasis">4 ps 杄af</span>
<span class="docEmphasis">5 history</span>
<span class="docEmphasis">6 more /etc/passwd</span>
<span class="docEmphasis">7 cd</span>
<span class="docEmphasis">8 echo $USER</span>
<span class="docEmphasis">9 set</span>
</pre>

<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText">The history list is displayed. Each command is numbered.</td>
  </tr>
</table>

<h5 id="ch09list15" class="docExampleTitle">Example 9.15 </h5>
<pre>% <span class="docEmphStrong">history 杊</span>       <span class="docEmphasis"># Print without line numbers</span>
<span class="docEmphasis">ls</span>
<span class="docEmphasis">vi file1</span>
<span class="docEmphasis">df</span>
<span class="docEmphasis">ps 杄af</span>
<span class="docEmphasis">history</span>
<span class="docEmphasis">more /etc/passwd</span>
<span class="docEmphasis">cd</span>
<span class="docEmphasis">echo $USER</span>
<span class="docEmphasis">set</span>
<span class="docEmphasis">history 杗</span>
</pre>

<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText">The history list is displayed without line numbers.</td>
  </tr>
</table>

<h5 id="ch09list16" class="docExampleTitle">Example 9.16 </h5>
<pre>% <span class="docEmphStrong">history 杛</span>       <span class="docEmphasis"># Print the history list in reverse</span>
<span class="docEmphasis">11 history 杛</span>
<span class="docEmphasis">10 history 杊</span>
 <span class="docEmphasis">9 set</span>
 <span class="docEmphasis">8 echo $USER</span>
 <span class="docEmphasis">7 cd</span>
 <span class="docEmphasis">6 more /etc/passwd</span>
 <span class="docEmphasis">5 history</span>
 <span class="docEmphasis">4 ps 杄af</span>
 <span class="docEmphasis">3 df</span>
 <span class="docEmphasis">2 vi file1</span>
 <span class="docEmphasis">1 ls</span>
</pre>

<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -