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

📄 _chapter 10.htm

📁 linux、unix初学者的必读书籍 详细讲述了shell编程方法与技巧
💻 HTM
📖 第 1 页 / 共 3 页
字号:
out and then logging back in.</p>
<h5 id="ch10list07" class="docExampleTitle">Example 10.7 </h5>
<pre>$ <span class="docEmphStrong">. .profile</span>
$ <span class="docEmphStrong">. .kshrc</span>
$ <span class="docEmphStrong">. $ENV</span>
</pre>

<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText">Normally a child process is started when commands are
    executed. The dot command executes each of the initialization files,
    <span class="docEmphasis">.profile,</span> the <span class="docEmphasis">ENV</span>
    file (.<span class="docEmphasis">kshrc</span>), in the current shell. Local
    and global variables in these files are defined within this shell.
    Otherwise, the user would have to log out and log back in to cause these
    files to be executed for the login shell. The dot command makes that
    unnecessary.</td>
  </tr>
</table>

<h4 class="docSection2Title" id="ch10lev2sec5">10.1.5 The Command Line</h4>
<p class="docText">After logging in, the Korn 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 (or
tokens), separated by whitespace (blanks and/or tabs), and terminated with a
newline, 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">jobs,</span> or
a shell script. The command may contain special characters, called
metacharacters, which the shell must interpret while parsing the command line.
If a command line is too long, 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 Order of Processing Commands.</b> The first word on
the command line is the command to be executed. The command may be a keyword, a
special built-in command or utility, a function, a script, or an executable
program. The command is executed according to its type in the following order:<span id="ENB10-1"><a class="docLink" href="#EN10-1"><sup>[1]</sup></a></span>
</p>

<span style="FONT-WEIGHT: bold">
<ol class="docList" type="1">
  <li><span style="FONT-WEIGHT: normal">
  <p class="docList">Keyword (such as <span class="docEmphasis">if, while, until</span>).</span></li>
  <li><span style="FONT-WEIGHT: normal">
  <p class="docList">Aliases (see <span class="docEmphasis">typeset 杅</span>).</span></li>
  <li><span style="FONT-WEIGHT: normal">
  <p class="docList">Built-in commands.</span></li>
  <li><span style="FONT-WEIGHT: normal">
  <p class="docList">Functions.</span></li>
  <li><span style="FONT-WEIGHT: normal">
  <p class="docList">Scripts and executables.</span></li>
</ol>
</span>
<p class="docText">The special built-in commands and functions are defined
within the shell, and therefore are executed from within the current shell,
making them much faster in execution. Scripts and executable programs such as
<span class="docEmphasis">ls</span> and <span class="docEmphasis">pwd</span> are
stored on disk, and the shell must locate them within the directory hierarchy by
searching the <span class="docEmphasis">PATH</span> environment variable; the
shell then forks a new shell that executes the script. To find out the type of
command you are using, use the built-in command, <span class="docEmphasis">
whence 杤</span> , or its alias, <span class="docEmphasis">type.</span> (See
<a class="docLink" href="#ch10list08">Example 10.8</a>.)</p>
<h5 id="ch10list08" class="docExampleTitle">Example 10.8 </h5>
<pre>$ <span class="docEmphStrong">type print</span>
<span class="docEmphasis">print is a shell builtin</span>
$ <span class="docEmphStrong">type test</span>
<span class="docEmphasis">test is a shell builtin</span>
$ <span class="docEmphStrong">type ls</span>
<span class="docEmphasis">ls is a tracked alias for /usr/bin/ls</span>
$ <span class="docEmphStrong">type type</span>
<span class="docEmphasis">type is an exported alias for whence -v</span>
$ <span class="docEmphStrong">type bc</span>
<span class="docEmphasis">bc is /usr/bin/bc</span>
$ <span class="docEmphStrong">type if</span>
<span class="docEmphasis">if is a keyword</span>
</pre>
<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 Korn 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. In shell scripts, you can
explicitly control the exit status by using the <span class="docEmphasis">exit</span>
command.</p>
<h5 id="ch10list09" class="docExampleTitle">Example 10.9 </h5>
<pre>1   $ <span class="docEmphStrong">grep &quot;ellie&quot; /etc/passwd</span>
    <span class="docEmphasis">ellie:GgMyBsSJavd16s:9496:40:Ellie Quigley:/home/jody/ellie</span>
2   $ <span class="docEmphStrong">echo $?</span>
    <span class="docEmphasis">0</span>

3   $ <span class="docEmphStrong">grep &quot;nicky&quot; /etc/passwd</span>
4   $ <span class="docEmphStrong">echo $?</span>
    <span class="docEmphasis">1</span>

5   $ <span class="docEmphStrong">grep &quot;scott&quot; /etc/passsswd</span>
    <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">ellie</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 one.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The <span class="docEmphasis">grep</span> fails because
      the file <span class="docEmphasis">/etc/passsswd</span> 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 two.</span></li>
    </ol>
    </span></td>
  </tr>
</table>

<p class="docText"><b>Multiple Commands and Command Grouping.</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="ch10list10" class="docExampleTitle">Example 10.10 </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 until the
    newline is reached. Commands may also be grouped so that all of the output
    is either piped to another command or redirected to a file.</td>
  </tr>
</table>

<h5 id="ch10list11" class="docExampleTitle">Example 10.11 </h5>
<pre>$ <span class="docEmphStrong">( ls ; pwd; date ) &gt; 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></td>
  </tr>
</table>

<p class="docText"><b>Conditional Execution of Commands.</b> With conditional
execution, two command strings are separated by two special metacharacters,
<span class="docEmphasis">&amp;&amp;</span> and ||. 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="ch10list12" class="docExampleTitle">Example 10.12 </h5>
<pre>$ <span class="docEmphStrong">cc prgm1.c 杘 prgm1 &amp;&amp; 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 <span class="docEmphasis">
    successful</span> (has a zero exit status), the command after the
    <span class="docEmphasis">&amp;&amp;</span> is executed.</td>
  </tr>
</table>

<h5 id="ch10list13" class="docExampleTitle">Example 10.13 </h5>
<pre>$ <span class="docEmphStrong">cc prog.c &gt;&amp; err || mail bob &lt; 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 <span class="docEmphasis">fails</span>
    (has a nonzero exit status), the command after the || is executed.</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 (&amp;) 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 should 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>
<h5 id="ch10list14" class="docExampleTitle">Example 10.14 </h5>
<pre>1   $ <span class="docEmphStrong">man ksh | 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 of the manual pages for the Korn shell 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">Two numbers 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, the process identification
      number, of this job.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The Korn shell prompt appears immediately. While your
      program is running in the background, the shell is waiting for another
      command in the foreground.</span></li>
    </ol>
    </span></td>
  </tr>
</table>

<h4 class="docSection2Title" id="ch10lev2sec6">10.1.6 Command Line History</h4>
<p class="docText">The history mechanism keeps a numbered record of the commands
that you have typed at the command line in a history file. You can recall a
command from the history file and reexecute it without retyping the command. The
<span class="docEmphasis">history</span> built-in command displays the history
list. The default name for the history file is <span class="docEmphasis">.sh_history,</span>
and it is located in your home directory.</p>
<p class="docText">The <span class="docEmphasis">HISTSIZE</span> variable,
accessed when <span class="docEmphasis">ksh</span> first accesses the history
file, specifies how many commands can be accessed from the history file. The
default size is 128. The <span class="docEmphasis">HISTFILE</span> variable
specifies the name of the command history file (<span class="docEmphasis">~/.sh_history</span>
is the default) where commands are stored. The history file grows from one login
session to the next; it becomes very large unless you clean it out. The history
command is a preset alias for the <span class="docEmphasis">fc 杔</span>
command.</p>
<p class="docText"><span class="docEmphStrong">The <span class="docEmphasis">
history</span> Command/Redisplay Commands.</span> The built-in
<span class="docEmphasis">history</span> command lists previously typed commands
preceded by a number. The command can take arguments to control the display.</p>
<h5 id="ch10list15" class="docExampleTitle">Example 10.15 </h5>
<pre>1   $ <span class="docEmphStrong">history</span>            <span class="docEmphasis"># Same as</span> fc 杔
    <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>
    <span class="docEmphasis">10 history</span>

2   $ <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>
    <span class="docEmphasis">history 杗</span>

3   $ <span class="docEmphStrong">history 8</span>          <span class="docEmphasis"># List from 8th command to present</span>
    <span class="docEmphasis">8   echo $USER</span>
    <span class="docEmphasis">9   set</span>
    <span class="docEmphasis">10  history</span>
    <span class="docEmphasis">11  history 杗</span>
    <span class="docEmphasis">12  history 8</span>

4   $ <span class="docEmphStrong">history 

⌨️ 快捷键说明

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