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

📄 _chapter 8.htm

📁 linux、unix初学者的必读书籍 详细讲述了shell编程方法与技巧
💻 HTM
📖 第 1 页 / 共 4 页
字号:
      <p class="docList">The <span class="docEmphasis">stty</span> command sets 
      terminal options. The erase key is set to <span class="docEmphasis">^h,</span> 
      so that when you press the Backspace key, the letter typed preceding the 
      cursor is erased.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">A function called <span class="docEmphasis">go</span> 
      is defined. The purpose of this function is to take one argument, a 
      directory name, <span class="docEmphasis">cd</span> to that directory, and 
      set the primary prompt to the present working directory. The
      <span class="docEmphasis">basename</span> command removes all but the last 
      entry of the path. The prompt will show you the current directory.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The <span class="docEmphasis">trap</span> command is a 
      signal handling command. When you exit the shell, that is, log out, the
      <span class="docEmphasis">.logout</span> file will be executed.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The <span class="docEmphasis">clear</span> command 
      clears the screen.</span></li>
    </ol>
    </span></td>
  </tr>
</table>
<p class="docText"><b>The Prompts.</b> When used interactively, the shell 
prompts you for input. When you see the prompt, you know that you can start 
typing commands. The Bourne shell provides two prompts: the primary prompt, a 
dollar sign (<span class="docEmphasis">$</span>), and the secondary prompt, a 
right angle bracket symbol (<span class="docEmphasis">&gt;</span>). The prompts are 
displayed when the shell is running interactively. You can change these prompts. 
The variable <span class="docEmphasis">PS1</span> is the primary prompt set 
initially to a dollar sign (<span class="docEmphasis">$</span>). The primary 
prompt appears when you log on and the shell waits for you to type commands. The 
variable <span class="docEmphasis">PS2</span> is the secondary prompt, initially 
set to the right angle bracket character. It appears if you have partially typed 
a command and then pressed the carriage return. You can change the primary and 
secondary prompts.</p>
<p class="docText"><b>The Primary Prompt.</b> The dollar sign is the default 
primary prompt. You can change your prompt. Normally prompts are defined in
<span class="docEmphasis">.profile,</span> the user initialization file.</p>
<h5 id="ch08list03" class="docExampleTitle">Example 8.3 </h5>
<pre>1   <span class="docEmphStrong">$ PS1=&quot;'uname -n &gt; '&quot;</span>
2   <span class="docEmphasis">chargers &gt;</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 default primary prompt is a dollar sign (<span class="docEmphasis">$</span>). 
      The <span class="docEmphasis">PS1</span> prompt is being reset to the name 
      of the machine (<span class="docEmphasis">uname 杗</span>) and a
      <span class="docEmphasis">&gt;</span> symbol. (Don't confuse backquotes and 
      single quotes.)</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The new prompt is displayed.</span></li>
    </ol>
    </span></td>
  </tr>
</table>
<p class="docText"><b>The Secondary Prompt.</b> The <span class="docEmphasis">
PS2</span> prompt is the secondary prompt. Its value is displayed to standard 
error, which is the screen by default. This prompt appears when you have not 
completed a command and have pressed the carriage return.</p>
<h5 id="ch08list04" class="docExampleTitle">Example 8.4 </h5>
<pre>1   $ echo &quot;Hello
2   <span class="docEmphStrong">&gt;</span> there&quot;
3   <span class="docEmphasis">Hello</span>
    <span class="docEmphasis">there</span>
4   $

5   <span class="docEmphStrong">$ PS2=&quot;枛枛&gt; &quot;</span>
6   <span class="docEmphStrong">$</span> echo 'Hi
7   <span class="docEmphStrong">枛枛枛&gt;</span>
    <span class="docEmphStrong">枛枛枛&gt;</span>
    <span class="docEmphStrong">枛枛枛&gt;</span> there'
    <span class="docEmphasis">Hi</span>


    <span class="docEmphasis">there</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 double quotes must be matched after the string &quot;<span class="docEmphasis">Hello.</span></span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">When a newline is entered, the secondary prompt 
      appears. Until the closing double quotes are entered, the secondary prompt 
      will be displayed.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The output of the <span class="docEmphasis">echo</span> 
      command is displayed.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The primary prompt is displayed.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The secondary prompt is reset.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The single quote must be matched after the string
      <span class="docEmphasis">'Hi.</span></span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">When a newline is entered, the new secondary prompt 
      appears. Until the closing single quote is entered, the secondary prompt 
      will be displayed.</span></li>
    </ol>
    </span></td>
  </tr>
</table>
<p class="docText"><b>The Search Path.</b> The <span class="docEmphasis">path</span> 
variable is used by the Bourne shell to locate commands typed at the command 
line. The path is a colon-separated list of directories used by the shell when 
searching for commands. The search is from left to right. The dot at the end of 
the path represents the current working directory. If the command is not found 
in any of the directories listed in the path, the Bourne shell sends to standard 
error the message <span class="docEmphasis">filename: not found.</span> It is 
recommended that the path be set in the <span class="docEmphasis">.profile</span> 
file.</p>
<p class="docText">If the dot is not included in the path and you are executing 
a command or script from the current working directory, the name of the script 
must be preceded with a <span class="docEmphasis">./,</span> such as
<span class="docEmphasis">./program_name,</span> so that shell can find the 
program.</p>
<h5 id="ch08list05" class="docExampleTitle">Example 8.5 </h5>
<pre>(Printing the PATH)
1   $ <span class="docEmphStrong">echo $PATH</span>
<span class="docEmphasis">/home/gsa12/bin:/usr/ucb:/usr/bin:/usr/local/bin:/usr/bin:/usr/local/bin:.</span>

(Setting the PATH)
2   $ <span class="docEmphStrong">PATH=$HOME:/usr/ucb:/usr:/usr/bin:/usr/local/bin:.</span>
3   $ <span class="docEmphStrong">export PATH</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">By echoing <span class="docEmphasis">$PATH,</span> the 
      value of the <span class="docEmphasis">PATH</span> variable is displayed. 
      The path consists of a list of colon-separated elements and is searched 
      from left to right. The dot at the end of the path represents the user's 
      current working directory.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">To set the path, a list of colon-separated directories 
      are assigned to the <span class="docEmphasis">PATH</span> variable.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">By exporting the path, child processes will have access 
      to it.</span></li>
    </ol>
    </span></td>
  </tr>
</table>

<p class="docText"><span class="docEmphStrong">The <span class="docEmphasis">
hash</span> Command.</span> The <span class="docEmphasis">hash</span> command 
controls the internal hash table used by the shell to improve efficiency in 
searching for commands. Instead of searching the path each time a command is 
entered, the first time you type a command, the shell uses the search path to 
find the command, and then stores it in a table in the shell's memory. The next 
time you use the same command, the shell uses the hash table to find it. This 
makes it much faster to access a command than having to search the complete 
path. If you know that you will be using a command often, you can add the 
command to the hash table. You can also remove commands from the table. The 
output of the <span class="docEmphasis">hash</span> command displays both the 
number of times the shell has used the table to find a command (<span class="docEmphasis">hits</span>) 
and the relative cost (<span class="docEmphasis">cost</span>) of looking up the 
command, that is, how far down the search path it had to go before it found the 
command. The <span class="docEmphasis">hash</span> command with the
<span class="docEmphasis">杛</span> option clears the hash table.</p>
<h5 id="ch08list06" class="docExampleTitle">Example 8.6 </h5>
<pre>1   $ <span class="docEmphStrong">hash</span>
    <span class="docEmphasis">hits  cost   command</span>
    <span class="docEmphasis">3     8      /usr/bin/date</span>
    <span class="docEmphasis">1     8      /usr/bin/who</span>
    <span class="docEmphasis">1     8      /usr/bin/ls</span>
2   $ <span class="docEmphStrong">hash vi</span>
    <span class="docEmphasis">3     8      /usr/bin/date</span>
    <span class="docEmphasis">1     8      /usr/bin/who</span>
    <span class="docEmphasis">1     8      /usr/bin/ls</span>
    <span class="docEmphasis">0     6      /usr/ucb/vi</span>
3   $ <span class="docEmphStrong">hash 杛</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">hash</span> command 
      displays the commands currently stored in the internal hash table. The 
      shell will not have to search the search path to find the commands listed 
      when they are entered at the command line. This saves time. Otherwise, the 
      shell has to go out to the disk to search the path. When you type a new 
      command, the shell will search the path first, and then place it on the 
      hash table. The next time you use that command, the shell finds it in 
      memory.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The <span class="docEmphasis">hash</span> command can 
      take arguments; the names of commands you want to guarantee get stored on 
      the hash table ahead of time.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The <span class="docEmphasis">hash</span> command with 
      the <span class="docEmphasis">杛</span> option clears the hash table.</span></li>
    </ol>
    </span></td>
  </tr>
</table>

<p class="docText"><span class="docEmphStrong">The <span class="docEmphasis">dot</span> 
Command.</span> The <span class="docEmphasis">dot</span> command is a built-in 
Bourne shell command. It takes a script name as an argument.The script will be 
executed in the environment of the current shell; that is, a child process will 
not be started. All variables set in the script will become part of the current 
shell's environment. Likewise, all variables set in the current shell will 
become part of the script's environment. The <span class="docEmphasis">dot</span> 
command is normally used to re-execute the <span class="docEmphasis">.profile</span> 
file if it has been modified. For example, if one of the settings, such as the
<span class="docEmphasis">EDITOR</span> or <span class="docEmphasis">TERM</span> 

⌨️ 快捷键说明

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