ch04_09.htm
来自「unix基础教程」· HTM 代码 · 共 1,119 行 · 第 1/5 页
HTM
1,119 行
<h4 class="refsect2">Examples</h4><blockquote><pre class="code">trap 'exec 2>&-' 0 <i class="lineannotation">Close standard error when </i> <i class="lineannotation">shell script exits (signal 0)</i>$ <tt class="userinput"><b>exec /bin/csh</b></tt> <i class="lineannotation">Replace Bourne shell with C shell</i>$ <tt class="userinput"><b>exec < infile</b></tt> <i class="lineannotation">Reassign standard input to infile</i></pre></blockquote></td></tr><tr><td valign="top"><a name="exit">exit</a></a></td><td><tt class="literal">exit</tt> [<tt><em class="replaceable"><tt>n</tt></em></tt>]<p><a name="IXT-4-122842" />Exit a shell script with status <em class="emphasis">n</em></tt> (e.g., <tt class="literal">exit 1</tt>).<em class="emphasis">n</em></tt> can be 0 (success) or nonzero (failure).If <em class="emphasis">n</em></tt> is not given, exit status is that of the most recent command.<tt class="literal">exit</tt> can be issued at the command line to close a window (log out).Exit statuses can range in value from 0 to 255.</p><h4 class="refsect2">Example</h4><blockquote><pre class="code">if [ $# -eq 0 ]then echo "Usage: $0 [-c] [-d] file(s)" 1>&2 exit 1 # Error statusfi</pre></blockquote></td></tr><tr><td valign="top"><a name="export">export</a></a></td><td><tt class="literal">export</tt> [<tt><em class="replaceable"><tt>variables</em></tt>]<br><tt class="literal">export</tt> [<tt><em class="replaceable">name</em></tt><tt class="literal">=</tt>[<tt><em class="replaceable">value</tt></em></tt>] ...]<br><tt class="literal">export -p</tt><p><a name="IXT-4-122843" />Pass (export) the value of one or more shell <em class="emphasis">variables</em></tt>, giving global meaning to the variables (which are local by default).For example, a variable defined in one shell script must beexported if its value is used in other programscalled by the script. If no <em class="emphasis">variables</em></tt> are given, <tt class="literal">export</tt> lists the variables exported by the current shell.Thesecond form is the Korn shell version, which is similar to the firstform except that you can set a variable <em class="emphasis">name</em></tt> to a <em class="emphasis">value</em></tt>before exporting it.The third form is specific to <tt class="literal">ksh93</tt>.</p><h4 class="refsect2">Option</h4><dl><dt><tt class="literal">-p</tt></dt><dd>Print <tt class="literal">export</tt> before printing thenames and values of exported variables.This allows saving a list of exported variables for rereading later.</p></dd></dl><h4 class="refsect2">Example</h4><p>In the Bourne shell, you would type:</p><blockquote><pre class="code">TERM=vt100export TERM</pre></blockquote><p>In the Korn shell, you could type this instead:</p><blockquote><pre class="code">export TERM=vt100</pre></blockquote></td></tr><tr><td valign="top"><a name="false">false</a></a></td><td><tt class="literal">false</tt><p><a name="IXT-4-122844" /><tt class="literal">ksh88</tt> alias for <tt class="literal">let 0</tt>.Built-in command in <tt class="literal">ksh93</tt> that exits witha false return value.</p></td></tr><tr><td valign="top"><a name="fc">fc</a></a></td><td><tt class="literal">fc</tt> [<em class="replaceable"><tt>options</em>] [<em class="replaceable">first</em> [<em class="replaceable">last</em>]]<br><tt class="literal">fc -e -</tt> [<em class="replaceable">old</em><tt class="literal">=</tt><em class="replaceable">new</em>] [<em class="replaceable">command</tt></em>]<p><a name="IXT-4-122845" /><tt class="literal">ksh88</tt> only. Display or edit commands in the history list.(Use only one of <tt class="literal">-l</tt> or <tt class="literal">-e</tt>.)<em class="emphasis">first</em> and <em class="emphasis">last</em> are numbers orstrings specifying the range of commands to display or edit.If <em class="emphasis">last</em> is omitted, <tt class="literal">fc</tt> applies to a single command (specifiedby <em class="emphasis">first</em>). If both <em class="emphasis">first</em> and <em class="emphasis">last</em> are omitted,<tt class="literal">fc</tt> edits the previous command or lists the last 16.The second form of <tt class="literal">fc</tt> takes a history <em class="emphasis">command</em>,replaces <em class="emphasis">old</em> string with <em class="emphasis">new</em> string,and executes the modified command. If no strings arespecified, <em class="emphasis">command</em> is just reexecuted. If no <em class="emphasis">command</em>is given either, the previous command is reexecuted.<em class="emphasis">command</em> is a number or string like <em class="emphasis">first</em>.See the examples in <a href="ch04_05.htm#unut-ch-4-sect-5">Section 4.5</a>.</p><h4 class="refsect2">Options</h4><dl><dt><tt class="literal">-e</tt> [<tt><em class="replaceable">editor</em></tt>]</dt><dd>Invoke <em class="emphasis">editor</em></tt> to edit the specified history commands.The default <em class="emphasis">editor</em></tt> is set by the shell variable FCEDIT.If that variable is notset, the default is <tt class="literal">/bin/ed</tt>.</p></dd><dt><tt class="literal">-e</tt> <tt class="literal">-</tt></dt><dd>Execute (or redo) a history command; refer to second syntax line above.</p></dd><dt><tt class="literal">-l</tt></dt><dd>List the specified command or range of commands, or list the last 16.</p></dd><dt><tt class="literal">-n</tt></dt><dd>Suppress command numbering from the <tt class="literal">-l</tt> listing.</p></dd><dt><tt class="literal">-r</tt></dt><dd>Reverse the order of the <tt class="literal">-l</tt> listing.</p></dd></dl></td></tr><tr><td valign="top"><a name="fc">fc</a></a></td><td><tt class="literal">fc</tt><p><tt class="literal">ksh93</tt> alias for <tt class="literal">hist</tt>.<a name="IXT-4-122846" /></p></td></tr><tr><td valign="top"><a name="fg">fg</a></a></td><td><tt class="literal">fg</tt> [<tt><em class="replaceable"><tt>jobIDs</tt></em></tt>]<p><a name="IXT-4-122847" />Bring current job or <em class="emphasis">jobIDs</em></tt> to the foreground. See <a href="ch04_06.htm#unut-ch-4-sect-6">Section 4.6</a></p></td></tr><tr><td valign="top"><a name="fi">fi</a></a></td><td><tt class="literal">fi</tt><p><a name="IXT-4-122848" />Reserved word that ends an <tt class="literal">if</tt> statement.(Don't forget to use it!)</p></td></tr><tr><td valign="top"><a name="for">for</a></a></td><td><tt class="literal">for x</tt> [<tt class="literal">in</tt> <tt><em class="replaceable"><tt>list</em></tt>]<br><tt class="literal">do </tt><br>    <tt><em class="replaceable">commands</tt></em></tt> <br><tt class="literal">done</tt><p><a name="IXT-4-122849" />For variable <em class="emphasis">x</em></tt> (in optional <em class="emphasis">list</em></tt> of values) do <em class="emphasis">commands</em></tt>.If <tt class="literal">in</tt> <em class="emphasis">list</em></tt> is omitted,<tt class="literal">"$@"</tt> (the positional parameters) is assumed.</p><h4 class="refsect2">Examples</h4><p>Paginate files specified on the command line; save each result:</p><blockquote><pre class="code">for file; do pr $file > $file.tmpdone</pre></blockquote><p>Search chapters for a list of words (like <tt class="literal">fgrep -f</tt>):</p><blockquote><pre class="code">for item in `cat program_list`do echo "Checking chapters for" echo "references to program $item..." grep -c "$item.[co]" chap*done</pre></blockquote><p>Extract a one-word title from each file and use as new filename:</p><blockquote><pre class="code">for filedo name=`sed -n 's/NAME: //p' $file` mv $file $namedone</pre></blockquote></td></tr><tr><td valign="top"><a name="for">for</a></a></td><td><tt class="literal">for ((</tt><tt><em class="replaceable"><tt>init</em></tt><tt class="literal">;</tt> <tt><em class="replaceable">cond</em></tt><tt class="literal">;</tt> <tt><em class="replaceable">incr</em></tt><tt class="literal">))</tt><br><tt class="literal">do</tt><br>    <tt><em class="replaceable">commands</tt></em></tt> <br><tt class="literal">done</tt><p><a name="IXT-4-122850" /><tt class="literal">ksh93</tt> only.Arithmetic <tt class="literal">for</tt> loop, similar to C's.Evaluate <em class="emphasis">init</em></tt>.While <em class="emphasis">cond</em></tt> is true, execute the body of the loop.Evaluate <em class="emphasis">incr</em></tt> before re-testing <em class="emphasis">cond</em></tt>.Any one of the expressions may be omitted; a missing <em class="emphasis">cond</em></tt>is treated as being true.</p><h4 class="refsect2">Examples</h4><p>Search for a phrase in each odd chapter:</p><blockquote><pre class="code">for ((x=1; x <= 20; x += 2))do grep $1 chap$xdone</pre></blockquote></td></tr><tr><td valign="top"><a name="function">function</a></a></td><td><tt class="literal">function</tt> <tt><em class="replaceable"><tt>name</em></tt> <tt class="literal">{</tt> <tt><em class="replaceable">commands</tt></em></tt><tt class="literal">; }</tt><p><a name="IXT-4-122851" />Korn shell only.Define <em class="emphasis">name</em></tt> as a shell function.See the description of semantic issues in the <em class="emphasis">name</em></tt><tt class="literal">()</tt> entry earlier.</p><h4 class="refsect2">Example</h4><p>Define a function to count files.</p><blockquote><pre class="code">$ <tt class="userinput"><b>function fcount {</b></tt>> <tt class="userinput"><b>ls | wc -l</b></tt>> <tt class="userinput"><b>}</b></tt></pre></blockquote></td></tr><tr><td valign="top"><a name="functions">functions</a></a></td><td><tt class="literal">functions</tt><p><a name="IXT-4-122852" />Korn shell alias for <tt class="literal">typeset -f</tt>.(Note the “s” in the name; <tt class="literal">function</tt>is a Korn shell keyword.)See <b class="emphasis-bold"><a href="#typeset">typeset</a></b> later in this listing.</p></td></tr><tr><td valign="top"><a name="getconf">getconf</a></a></td><td><p><tt class="literal">getconf</tt> [<tt><em class="replaceable"><tt>name</em></tt> [<tt><em class="replaceable">path</tt></em></tt>]]</p><p><a name="IXT-4-122853" /><tt class="literal">ksh93</tt> only.Retrieve the values for parameters that can vary acrosssystems. <em class="emphasis">name</em></tt> is the parameter to retrieve;<em class="emphasis">path</em></tt> is a filename to test for parameters thatcan vary on different filesystem types.</p><p>The parameters are defined by the POSIX 1003.1 and 1003.2 standards.See the entry for <b class="emphasis-bold"><a href="#getconf">getconf</a></b> in <a href="ch02_01.htm">Chapter 2</a>.</p><h4 class="refsect2">Example</h4><p>Print the maximum value that can be held in a C <tt class="literal">int</tt>.</p><blockquote><pre class="code">$ <tt class="userinput"><b>getconf INT_MAX</b></tt>2147483647</pre></blockquote></td></tr><tr><td valign="top"><a name="getopts">getopts</a></a></td><td><tt class="literal">getopts</tt> [<tt class="literal">-a</tt> <tt><em class="replaceable"><tt>name</em></tt>] <tt><em class="replaceable">string</em></tt> <tt><em class="replaceable">name</em></tt> [<tt><em class="replaceable">args</tt></em></tt>]<p><a name="IXT-4-122854" />Process command-line arguments (or <em class="emphasis">args</em></tt>,if specified) and checkfor legal options. <tt class="literal">getopts</tt>is used in shell script loops and is intended to ensure standard syntax forcommand-line options. Standard syntax dictates that command-lineoptions begin with a <tt class="literal">+</tt> or a <tt class="literal">-</tt>. Options can bestacked; i.e., consecutive letters can follow a single <tt class="literal">-</tt>. Endprocessing of options by specifying <tt class="literal">--</tt> onthe command line. <em class="emphasis">string</em></tt> contains the option lettersto be recognized by <tt class="literal">getopts</tt> when runningthe shell script. Valid options are processedin turn and stored in the shell variable <em class="emphasis">name</em></tt>. If an option is followed by acolon, the option must be followed by one or more arguments. (Multiple arguments must be given to the command as one shell <em class="emphasis">word</em></tt>.This is done by quoting the arguments or separating them with commas.The application must be written to expect multiple arguments in thisformat.)<tt class="literal">getopts</tt> uses the shell variables OPTARG and OPTIND.<tt class="literal">getopts</tt> is available to non-Bourne shell users as<tt class="literal">/usr/bin/getopts</tt>.</p><h4 class="refsect2">Option</h4><dl><dt><tt class="literal">-a</tt></dt><dd>Use <em class="emphasis">name</em></tt> in error messages about invalidoptions. <tt class="literal">ksh93</tt> only.</p></dd></dl></td></tr><tr><td valign="top"><a name="hash">hash</a></a></td><td><tt class="literal">hash</tt> [<tt class="literal">-r</tt>] [<em class="replaceable"><tt>commands</tt></em>]<p><a name="IXT-4-122855" />Bourne shell version.As the shell finds commands along the search path ($PATH),it remembers the found location in an internal hash table.The next time you enter a command, the shell uses the value storedin its hash table.</p><p>With no arguments, <tt class="literal">hash</tt> lists the current hashed commands.The display shows <em class="emphasis">hits</em> (the number of times the commandis called by the shell) and <em class="emphasis">cost</em> (the level of work neededto find the command).Commands that were found in a relative directory have anasterisk (<tt class="literal">*</tt>) added in the <em class="emphasis">hits</em> column.</p><p>With <em class="emphasis">commands</em>, the shell will add those commands to thehash table.</p><p><tt class="literal">-r</tt> removescommands from the hash list, either all of them or just the specified<em class="emphasis">commands</em>.The hash table is also cleared when PATH is assigned.Use <tt class="literal">PATH=$PATH</tt> to clear the hash table withoutaffecting your search path.This is most useful if you have installed a new version of a commandin a directory that is earlier in $PATH than the currentversion of the command.</p>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?