ch04_09.htm
来自「unix基础教程」· HTM 代码 · 共 1,119 行 · 第 1/5 页
HTM
1,119 行
(This allows replacement of system commands with faster, built-in versions.)Otherwise, the built-in command is always found.</p><h4 class="refsect2">Options</h4><dl><dt><tt class="literal">-d</tt></dt><dd>Delete the built-in command <em class="emphasis">name</em></tt>.</p></dd><dt><tt class="literal">-f</tt></dt><dd>Load new built-in command from <em class="emphasis">library</em></tt>.</p></dd><dt><tt class="literal">-s</tt></dt><dd>Onlyprint “special” built-ins (those designatedas special by POSIX).</p></dd></dl></td></tr><tr><td valign="top"><a name="case">case</a></a></td><td><br><tt class="literal">case</tt> <em class="replaceable"><tt>value</em> <tt class="literal">in</tt><br>    <em class="replaceable">pattern1</em><tt class="literal">)</tt> <em class="replaceable">cmds1</em><tt class="literal">;;</tt><br>    <em class="replaceable">pattern2</em><tt class="literal">)</tt> <em class="replaceable">cmds2</tt></em><tt class="literal">;;</tt><br>    .<br>    .<br>    .<br><tt class="literal">esac</tt><p><a name="IXT-4-122830" />Execute the first set of commands (<em class="emphasis">cmds1</em>) if <em class="emphasis">value</em> matches<em class="emphasis">pattern1</em>, execute the second set of commands (<em class="emphasis">cmds2</em>) if <em class="emphasis">value</em> matches <em class="emphasis">pattern2</em>, etc.Be sure the last command in each set ends with <tt class="literal">;;</tt>.<em class="emphasis">value</em> is typically a positional parameter or other shell variable.<em class="emphasis">cmds</em> are typically Unix commands, shell programming commands,or variable assignments.Patterns can use file-generation metacharacters.Multiple patterns (separated by <tt class="literal">|</tt>) can be specified on the same line;in this case, the associated <em class="emphasis">cmds</em> are executed whenever <em class="emphasis">value</em> matches any of these patterns.See the Examples here and under <tt class="literal">eval</tt>.</p><h4 class="refsect2">Korn Shell Notes</h4><ul><li><p>The Korn shell allows <em class="emphasis">pattern</em> to be preceded by an optionalopen parenthesis, as in <tt class="literal">(</tt><em class="replaceable"><tt>pattern</tt></em><tt class="literal">)</tt>.It's useful for balancingparentheses inside a <tt class="literal">$( )</tt> construct.</p></li><li><p>The Korn shell also allows a case to end with <tt class="literal">;&</tt>instead of <tt class="literal">;;</tt>.In such cases control “falls through” to the group of statements forthe next <em class="emphasis">pattern</em>.</p></li></ul><h4 class="refsect2">Examples</h4><p>Checkfirst command-line argument and take appropriate action:</p><blockquote><pre class="code">case $1 in # Match the first arg no|yes) response=1;; -[tT]) table=TRUE;; *) echo "unknown option"; exit 1;;esac</pre></blockquote><p>Read user-supplied lines until user exits:</p><blockquote><pre class="code">while : # Null command; always truedo echo "Type . to finish ==> \c" read line case "$line" in .) echo "Message done" break ;; *) echo "$line" >> $message ;; esacdone</pre></blockquote></td></tr><tr><td valign="top"><a name="cd">cd</a></a></td><td><tt class="literal">cd</tt> [<em class="replaceable"><tt>dir</em>]<br><tt class="literal">cd</tt> [<tt class="literal">-LP</tt>] [<em class="replaceable">dir</em>]<br><tt class="literal">cd</tt> [<tt class="literal">-LP</tt>] [<tt class="literal">-</tt>]<br><tt class="literal">cd</tt> [<tt class="literal">-LP</tt>] [<em class="replaceable">old new</tt></em>]<p><a name="IXT-4-122831" /><a name="IXT-4-122832" />With no arguments, change to home directory of user.Otherwise, change working directory to <em class="emphasis">dir</em>. If <em class="emphasis">dir</em> is arelative pathname but is not in the current directory, the CDPATHvariable is searched. The last three command forms are specific to the Korn shell, where <tt class="literal">-</tt> stands for the previous directory.The fourth syntax modifies the current directory nameby replacing string <em class="emphasis">old</em> with <em class="emphasis">new</em> andthen switches to the resulting directory.</p><h4 class="refsect2">Options</h4><dl><dt><tt class="literal">-L</tt></dt><dd>Use the logical path (what the user typed, including any symbolic links)for <tt class="literal">cd ..</tt>and the value of PWD.This is the default.</p></dd><dt><tt class="literal">-P</tt></dt><dd>Use the actual filesystem physical pathfor <tt class="literal">cd ..</tt>and the value of PWD.</p></dd></dl><h4 class="refsect2">Example</h4><blockquote><pre class="code">$ <tt class="userinput"><b>pwd</b></tt>/var/spool/cron$ <tt class="userinput"><b>cd cron uucp</b></tt> <i class="lineannotation">cd prints the new directory</i>/var/spool/uucp</pre></blockquote></td></tr><tr><td valign="top"><a name="command">command</a></a></td><td><tt class="literal">command</tt> [<tt class="literal">-pvV</tt>] <em class="replaceable"><tt>name</em> [<em class="replaceable">arg</tt></em> ...]<p><a name="IXT-4-122833" /><tt class="literal">ksh93</tt> only.Without <tt class="literal">-v</tt> or <tt class="literal">-V</tt>,execute <em class="emphasis">name</em> with given arguments.This command bypasses any aliases or functions that may bedefined for <em class="emphasis">name</em>.</p><h4 class="refsect2">Options</h4><dl><dt><tt class="literal">-p</tt></dt><dd>Use a predefined, default search path, not the current value of PATH.</p></dd><dt><tt class="literal">-v</tt></dt><dd>Just like <tt class="literal">whence</tt>.</p></dd><dt><tt class="literal">-V</tt></dt><dd>Just like <tt class="literal">whence -v</tt>.</p></dd></dl><h4 class="refsect2">Example</h4><p>Createan alias for <tt class="literal">rm</tt> that will get the system'sversion, and run it with the <tt class="literal">-i</tt> option:</p><blockquote><pre class="code"><tt class="userinput"><b>alias 'rm=command -p rm -i'</b></tt></pre></blockquote></td></tr><tr><td valign="top"><a name="continue">continue</a></a></td><td><tt class="literal">continue</tt> [<tt><em class="replaceable"><tt>n</tt></em></tt>]<p><a name="IXT-4-122834" />Skip remaining commands in a <tt class="literal">for</tt>,<tt class="literal">while</tt>,<tt class="literal">select</tt>,or <tt class="literal">until</tt> loop,resuming with the next iteration of the loop(or skipping <em class="emphasis">n</em></tt> loops).</p></td></tr><tr><td valign="top"><a name="disown">disown</a></a></td><td><tt class="literal">disown</tt> [<tt><em class="replaceable"><tt>job</tt></em></tt> ...]<p><a name="IXT-4-122835" /><tt class="literal">ksh93</tt> only.When a login shell exits, do not send a <tt class="literal">SIGHUP</tt>to the given jobs.If no jobs are listed, no background jobswill receive <tt class="literal">SIGHUP</tt>.</p></td></tr><tr><td valign="top"><a name="do">do</a></a></td><td><tt class="literal">do</tt><p><a name="IXT-4-122836" />Reserved word that precedes the command sequence in a<tt class="literal">for</tt>, <tt class="literal">while</tt>, <tt class="literal">until</tt>, or <tt class="literal">select</tt> statement.</p></td></tr><tr><td valign="top"><a name="done">done</a></a></td><td><tt class="literal">done</tt><p><a name="IXT-4-122837" />Reserved word that ends a <tt class="literal">for</tt>, <tt class="literal">while</tt>, <tt class="literal">until</tt>, or <tt class="literal">select</tt> statement.</p></td></tr><tr><td valign="top"><a name="echo">echo</a></a></td><td><tt class="literal">echo</tt> [<tt class="literal">-n</tt>] [<tt><em class="replaceable"><tt>string</tt></em></tt>]<p><a name="IXT-4-122838" />Write <em class="emphasis">string</em></tt> to standard output; if <tt class="literal">-n</tt> isspecified, the output is not terminated by a newline.If no <em class="emphasis">string</em></tt> is supplied, echo a newline.In the Korn shell, <tt class="literal">echo</tt> is built-in, and itemulates the system's real <tt class="literal">echo</tt> command.<a href="#FOOTNOTE-6">[6]</a>(See also <b class="emphasis-bold"><a href="#echo">echo</a></b> in <a href="ch02_01.htm">Chapter 2</a>.)<tt class="literal">echo</tt> understands special escape characters,which must be quoted (or escaped with a \)to prevent interpretation by the shell:</p><blockquote class="footnote"><a name="FOOTNOTE-6" /><p> [6]But, if a path search finds <tt class="literal">/usr/bin/echo</tt>, the<tt class="literal">ksh</tt> built-in <tt class="literal">echo</tt> doesn't accept the <tt class="literal">-n</tt> option.(The situation with <tt class="literal">echo</tt> is a mess; consider using <tt class="literal">printf</tt> instead.)</p></blockquote><dl><dt><tt class="literal">\a</tt></dt><dd>Alert (ASCII BEL).(Not in <tt class="literal">/bin/sh</tt>'s <tt class="literal">echo</tt>.)</p></dd><dt><tt class="literal">\b</tt></dt><dd>Backspace.</p></dd><dt><tt class="literal">\c</tt></dt><dd>Suppress the terminating newline (same as <tt class="literal">-n</tt>).</p></dd><dt><tt class="literal">\f</tt></dt><dd>Formfeed.</p></dd><dt><tt class="literal">\n</tt></dt><dd>Newline.</p></dd><dt><tt class="literal">\r</tt></dt><dd>Carriage return.</p></dd><dt><tt class="literal">\t</tt></dt><dd>Tab character.</p></dd><dt><tt class="literal">\v</tt></dt><dd>Vertical-tab character.</p></dd><dt><tt class="literal">\\</tt></dt><dd>Backslash.</p></dd><dt><tt class="literal">\0</tt><tt><em class="replaceable">nnn</em></tt></dt><dd>ASCII character represented by octal number <em class="emphasis">nnn</em></tt>, where <em class="emphasis">nnn</em></tt>is one, two, or three digits and is preceded by a 0.</p></dd></dl><h4 class="refsect2">Examples</h4><blockquote><pre class="code">$ <tt class="userinput"><b>echo "testing printer" | lp</b></tt>$ <tt class="userinput"><b>echo "Warning: ringing bell \a"</b></tt></pre></blockquote></td></tr><tr><td valign="top"><a name="esac">esac</a></a></td><td><tt class="literal">esac</tt><p><a name="IXT-4-122839" />Reserved word that ends a <tt class="literal">case</tt> statement.Omitting <tt class="literal">esac</tt> is a common programming error.</p></td></tr><tr><td valign="top"><a name="eval">eval</a></a></td><td><tt class="literal">eval</tt> <tt><em class="replaceable"><tt>args</tt></em></tt><p><a name="IXT-4-122840" />Typically, <tt class="literal">eval</tt> is used in shell scripts,and <em class="emphasis">args</em></tt> is a line of code that contains shell variables. <tt class="literal">eval</tt> forces variable expansion to happen firstand then runs the resulting command.This “double-scanning” is useful any time shell variables contain input/output redirection symbols, aliases, or other shell variables.(For example, redirection normally happens before variable expansion, so a variable containing redirection symbols must beexpanded first using <tt class="literal">eval</tt>; otherwise, the redirection symbols remain uninterpreted.) See the C shell <tt class="literal">eval</tt>(<a href="ch05_01.htm">Chapter 5</a>) for another example.</p><h4 class="refsect2">Example</h4><p>This fragment of a Bourne shell script shows how <tt class="literal">eval</tt>constructs a command that isinterpreted in the right order:</p><blockquote><pre class="code">for optiondo case "$option" in <i class="lineannotation">Define where output goes</i> save) out=' > $newfile' ;; show) out=' | more' ;; esacdoneeval sort $file $out</pre></blockquote></td></tr><tr><td valign="top"><a name="exec">exec</a></a></td><td><p><tt class="literal">exec</tt> [<tt><em class="replaceable"><tt>command</em></tt> <tt><em class="replaceable">args</em></tt> ...]</p><p><tt class="literal">exec</tt> [<tt class="literal">-a</tt> <tt><em class="replaceable">name</em></tt>] [<tt class="literal">-c</tt>] [<tt><em class="replaceable">command</em></tt> <tt><em class="replaceable">args</tt></em></tt> ... ]</p><p><a name="IXT-4-122841" />Execute <em class="emphasis">command</em></tt> in place of the current process(instead of creating a new process). <tt class="literal">exec</tt> is also useful foropening, closing, or copying file descriptors.The second form is for <tt class="literal">ksh93</tt> only.</p><h4 class="refsect2">Options</h4><dl><dt><tt class="literal">-a</tt></dt><dd>Use <em class="emphasis">name</em></tt> for the valueof <tt class="literal">argv[0]</tt>.</p></dd><dt><tt class="literal">-c</tt></dt><dd>Clear the environment before executing the program.</p></dd></dl>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?