ch04_09.htm
来自「unix基础教程」· HTM 代码 · 共 1,119 行 · 第 1/5 页
HTM
1,119 行
<html><head><title>Built-in Commands (Bourne and Korn Shells) (UNIX in a Nutshell: System V Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Arnold Robbins" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="1-56592-427-4" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="UNIX in a Nutshell: System V Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Book Title" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch04_08.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch05_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">4.9. Built-in Commands (Bourne and Korn Shells)</h2><p><a name="unut-ch-4-ix-ksh-korn-shell-built-in-commands-list-of" /><a name="unut-ch-4-ix-sh-bourne-shell-built-in-commands-list-of" /><a name="unut-ch-4-ix-commands-for-sh-and-ksh-shells-sh-and-ksh-shells" />Examples to be entered as a command lineare shown with the <tt class="literal">$</tt> prompt. Otherwise,examples should be treated as code fragments that might be included in a shell script.For convenience, some of the reserved words used by multiline commandsare also included.</p><table border="1" cellpadding="5"><tr><td valign="top"><a name="!">!</a></a></td><td><p><tt class="literal">!</tt> <em class="replaceable"><tt>pipeline</tt></em></p><p><tt class="literal">ksh93</tt> only.Negate the sense of a pipeline.Returns an exit status of 0 if the pipeline exited nonzero,and an exit status of 1 if the pipeline exited zero.Typically used in <tt class="literal">if</tt> and<tt class="literal">while</tt> statements.</p><h4 class="refsect2">Example</h4><p>This code prints a message if user <tt class="literal">jane</tt>is not logged on:</p><blockquote><pre class="code">if ! who | grep jane > /dev/nullthen echo jane is not currently logged onfi</pre></blockquote></td></tr><tr><td valign="top"><a name="#">#</a></a></td><td><tt class="literal">#</tt><p><a name="IXT-4-122817" /><a name="IXT-4-122818" />Ignore all text that follows on the same line. <tt class="literal">#</tt> is used in shellscripts as the comment character and is not really a command. (Takecare when commenting a Bourne shell script.A file that has<tt class="literal">#</tt> as its first character is sometimes interpreted byolder systems as a C shell script.)</p></td></tr><tr><td valign="top"><a name="#!shell">#!shell</a></a></td><td><tt class="literal">#!</tt><em class="replaceable"><tt>shell</em> [<em class="replaceable">option</tt></em>]<p><a name="IXT-4-122819" /><a name="IXT-4-122820" />Used as the first line of a script toinvoke the named <em class="emphasis">shell</em>.Anything given on the rest of the line is passed<em class="emphasis">as a single argument</em> to the named <em class="emphasis">shell</em>.This feature is typically implemented by the kernel, but may notbe supported on some older systems.Some systems havea limit of around 32 characters on the maximum lengthof <em class="emphasis">shell</em>.For example:</p><blockquote><pre class="code">#!/bin/sh</pre></blockquote></td></tr><tr><td valign="top"><a name=":">:</a></a></td><td><tt class="literal">:</tt><p><a name="IXT-4-122821" />Null command. Returns an exit status of 0.Sometimes used on older systemsas the first character in a file to denote a Bourneshell script.See this Example and under <b class="emphasis-bold"><a href="#case">case</a></b>.The line is still processed for side effects, such asvariable and command substitutions.</p><h4 class="refsect2">Example</h4><p>Check whether someone is logged in:</p><blockquote><pre class="code">if who | grep $1 > /dev/nullthen : # Do nothing if pattern is foundelse echo "User $1 is not logged in"fi</pre></blockquote></td></tr><tr><td valign="top"><a name=".">.</a></a></td><td><p><tt class="literal">.</tt> <em class="replaceable"><tt>file</em> [<em class="replaceable">arguments</tt></em>]</p><p>Read and execute lines in <em class="emphasis">file</em>.<em class="emphasis">file</em> does not have to beexecutable but must reside in a directory searched by PATH.The Korn shell supports <em class="emphasis">arguments</em> that are stored in the positional parameters.</p></td></tr><tr><td valign="top"><a name="[[ ]]">[[ ]]</a></a></td><td><p><tt class="literal">[[</tt> <em class="replaceable"><tt>expression</tt></em> <tt class="literal">]]</tt></p><p><a name="IXT-4-122822" />Korn shell only. Same as <tt class="literal">test</tt> <em class="emphasis">expression</em> or <tt class="literal">[</tt> <em class="emphasis">expression</em> <tt class="literal">]</tt>,except that <tt class="literal">[[ ]]</tt> allows additional operators. Wordsplitting and filename expansion are disabled.Note that the brackets (<tt class="literal">[ ]</tt>) are typed literally, andthat they must be surrounded by whitespace.</p><h4 class="refsect2">Additional Operators</h4><table border="1" cellpadding="3"><tr><td><tt class="literal">&&</tt></td><td><p>Logical AND of test expressions (short circuit).</p></td></tr><tr><td><tt class="literal">||</tt></td><td><p>Logical OR of test expressions (short circuit).</p></td></tr><tr><td><tt class="literal"><</tt></td><td><p>First string is lexically “less than” the second.</p></td></tr><tr><td><tt class="literal">></tt></td><td><p>First string is lexically “greater than” the second. </p></td></tr></table><p></td></tr><tr><td valign="top"><a name="name()">name()</a></a></td><td><em class="replaceable"><tt>name</em> <tt class="literal">() { </tt><em class="replaceable">commands</tt></em><tt class="literal">; }</tt><p>Define <em class="emphasis">name</em> as a function. Syntax can be written onone line or across many. Since the Bourne shell has noaliasing capability, simple functions can serve as aliases.The Korn shell provides the <tt class="literal">function</tt> keyword,an alternateform that works the same way.</p><p>There are semantic differences that should be kept in mind:</p><ul><li><p>In the Bourne shell, all functionsshare traps with the “parent” shell andmay not be recursive.</p></li><li><p>In <tt class="literal">ksh88</tt>, all functionshave their own traps and local variables, and maybe recursive.</p></li><li><p>In <tt class="literal">ksh93</tt>, <em class="emphasis">name</em> <tt class="literal">()</tt> functionsshare traps with the “parent” shell andmay not be recursive.</p></li><li><p>In <tt class="literal">ksh93</tt>, <tt class="literal">function</tt> functionshave their own traps and local variables, and maybe recursive.Using the <tt class="literal">.</tt> commandwith a <tt class="literal">function</tt> function gives itBourne shell semantics.</p></li></ul><h4 class="refsect2">Example</h4><blockquote><pre class="code">$ <tt class="userinput"><b>count () {</b></tt>> <tt class="userinput"><b>ls | wc -l</b></tt>> <tt class="userinput"><b>}</b></tt></pre></blockquote><p>When issued at the command line, <tt class="literal">count</tt> now displays thenumber of files in the current directory.</p></td></tr><tr><td valign="top"><a name="alias">alias</a></a></td><td><tt class="literal">alias</tt> [<em class="replaceable"><tt>options</em>] [<em class="replaceable">name</em>[<tt class="literal">='</tt><em class="replaceable">cmd</tt></em><tt class="literal">'</tt>]]<p><a name="IXT-4-122823" /><a name="IXT-4-122824" /><a name="IXT-4-122825" />Korn shell only.Assign a shorthand <em class="emphasis">name</em> as a synonym for <em class="emphasis">cmd</em>.If <tt class="literal">='</tt><em class="emphasis">cmd</em><tt class="literal">'</tt> is omitted, print the alias for <em class="emphasis">name</em>;if <em class="emphasis">name</em> is also omitted, print all aliases.If the alias value contains a trailing space, the next word on the commandline also becomes a candidate for alias expansion.See also <b class="emphasis-bold"><a href="#unalias">unalias</a></b>.</p><p>These aliases are built into <tt class="literal">ksh88</tt>.Some use names of existing Bourne shell or C shell commands (which points out the similarities among the shells).</p><blockquote><pre class="code">autoload='typeset -fu'false='let 0'functions='typeset -f'hash='alias -t'history='fc -l'integer='typeset -i'nohup='nohup 'r='fc -e -'true=':'type='whence -v'</pre></blockquote><p>The following aliases are built into <tt class="literal">ksh93</tt>:</p><blockquote><pre class="code">autoload='typeset -fu'command='command 'fc='hist'float='typeset -E'functions='typeset -f'hash='alias -t --'history='hist -l'integer='typeset -i'nameref='typeset -n'nohup='nohup 'r='hist -s'redirect='command exec'stop='kill -s STOP'times='{ {time;} 2>&1;}'type='whence -v'</pre></blockquote><h4 class="refsect2">Options</h4><dl><dt><tt class="literal">-p</tt></dt><dd>Print the word <tt class="literal">alias</tt> before each alias.<tt class="literal">ksh93</tt> only.</p></dd><dt><tt class="literal">-t</tt></dt><dd>Create a tracked alias for a Unix command <em class="emphasis">name</em></tt>.The Korn shell remembers the full pathname of the command,allowing it to be found more quickly and to be issued from any directory.If no name is supplied, current tracked aliases are listed.Tracked aliases are the similar to hashed commands in the Bourne shell.</p></dd><dt><tt class="literal">-x</tt></dt><dd>Export the alias; it can now be used in shell scripts and othersubshells. If no name is supplied, current exported aliases are listed.</p></dd></dl><h4 class="refsect2">Example</h4><blockquote><pre class="code">alias dir='echo ${PWD##*/}'</pre></blockquote></td></tr><tr><td valign="top"><a name="autoload">autoload</a></a></td><td><tt class="literal">autoload</tt> [<tt><em class="replaceable"><tt>functions</tt></em></tt>]<p><a name="IXT-4-122826" />Load (define) the <em class="emphasis">functions</em></tt> only when they are first used.Korn shell alias for <tt class="literal">typeset -fu</tt>.</p></td></tr><tr><td valign="top"><a name="bg">bg</a></a></td><td><tt class="literal">bg</tt> [<tt><em class="replaceable"><tt>jobIDs</tt></em></tt>]<p><a name="IXT-4-122827" />Put current job or <em class="emphasis">jobIDs</em></tt> in the background. See <a href="ch04_06.htm#unut-ch-4-sect-6">Section 4.6</a>.</p></td></tr><tr><td valign="top"><a name="break">break</a></a></td><td><tt class="literal">break</tt> [<tt><em class="replaceable"><tt>n</tt></em></tt>]<p><a name="IXT-4-122828" />Exit from a <tt class="literal">for</tt><tt class="literal">while</tt>,<tt class="literal">select</tt>,or <tt class="literal">until</tt> loop(or break out of <em class="emphasis">n</em></tt> loops).</p></td></tr><tr><td valign="top"><a name="builtin">builtin</a></a></td><td><tt class="literal">builtin</tt> [ <tt class="literal">-ds</tt> ] [ <tt class="literal">-f</tt> <tt><em class="replaceable"><tt>library</em></tt> ] [ <tt><em class="replaceable">name</tt></em></tt> ... ]<p><a name="IXT-4-122829" /><tt class="literal">ksh93</tt> only.This command allows you to load new built-in commands into the shellat runtime from shared library files.</p><p>If no arguments are given, <tt class="literal">builtin</tt>prints all the built-in command names.With arguments, <tt class="literal">builtin</tt> adds each <em class="emphasis">name</em></tt>as a new built-in command (like <tt class="literal">cd</tt> or <tt class="literal">pwd</tt>).If the <em class="emphasis">name</em></tt> contains a slash, the newly-addedbuilt-in version is used only if a path search would otherwise have found acommand of the same name.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?