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

📄 ch05_08.htm

📁 unix基础教程
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<html><head><title>Built-in C Shell Commands (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 &amp; 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="ch05_07.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="part2.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">5.8. Built-in C Shell Commands</h2><p><a name="unut-ch-5-ix-csh-c-shell-built-in-commands-list-of" /><a name="unut-ch-5-ix-commands-for-csh-shell-csh-shell" /></p><table border="1" cellpadding="5"><tr><td valign="top"><a name="#">#</a></a></td><td><p><tt class="literal">#</tt></p><p><a name="IXT-5-123028" />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.In addition,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="#!">#!</a></a></td><td><tt class="literal">#!</tt><em class="replaceable"><tt>shell</em> [<em class="replaceable">option</tt></em>]<p><a name="IXT-5-123029" /><a name="IXT-5-123030" />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/csh -f</pre></blockquote></td></tr><tr><td valign="top"><a name=":">:</a></a></td><td><tt class="literal">:</tt><p><a name="IXT-5-123031" />Null (do-nothing) command.  Returns an exit status of 0.</p></td></tr><tr><td valign="top"><a name="alias">alias</a></a></td><td><tt class="literal">alias</tt> [<em class="replaceable"><tt>name</em> [<em class="replaceable">command</tt></em>]]<p><a name="IXT-5-123032" /><a name="IXT-5-123033" /><a name="IXT-5-123034" />Assign <em class="emphasis">name</em> as the shorthand name, or alias, for <em class="emphasis">command</em>. If <em class="emphasis">command</em> is omitted, print the alias for <em class="emphasis">name</em>; if <em class="emphasis">name</em> is also omitted, print all aliases.  Aliases can be definedon the command line, but they are more often stored in <tt class="literal">.cshrc</tt> so that they take effect after login.  (See <a href="ch05_03.htm#unut-ch-5-sect-3.4">Section 5.3.4</a> earlier in this chapter.)Alias definitions can reference command-line arguments, much likethe history list.  Use <tt class="literal">\!*</tt> to refer to all command-line arguments, <tt class="literal">\!^</tt> for the first argument, <tt class="literal">\!$</tt> for the last, etc.  An alias <em class="emphasis">name</em> can be any valid Unix command; however, you lose the originalcommand's meaning unless you type <em class="emphasis">\name</em>.  See also<b class="emphasis-bold"><a href="#unalias">unalias</a></b>.</p><h4 class="refsect2">Examples</h4><p>Set the size for <tt class="literal">xterm</tt> windows under the X Window System:</p><blockquote><pre class="code">alias R 'set noglob; eval `resize`; unset noglob'</pre></blockquote><p>Show aliases that contain the string <em class="emphasis">ls</em>:</p><blockquote><pre class="code">alias | grep ls</pre></blockquote><p>Run <tt class="literal">nroff</tt> on all command-line arguments:</p><blockquote><pre class="code">alias ms 'nroff -ms \!*'</pre></blockquote><p>Copy the file that is named as the first argument:</p><blockquote><pre class="code">alias back 'cp \!^ \!^.old'</pre></blockquote><p>Use the regular <tt class="literal">ls</tt>, not its alias:</p><blockquote><pre class="code">% <tt class="userinput"><b>\ls</b></tt></pre></blockquote></td></tr><tr><td valign="top"><a name="bg">bg</a></a></td><td><tt class="literal">bg</tt> [<em class="replaceable"><tt>jobIDs</tt></em>]<p><a name="IXT-5-123035" />Put the current job or the <em class="emphasis">jobIDs</em>in the background. See <a href="ch05_06.htm#unut-ch-5-sect-6">Section 5.6</a>.</p><h4 class="refsect2">Example</h4><p>To place a time-consuming process in the background, you mightbegin with:</p><blockquote><pre class="code">4% <tt class="userinput"><b>nroff -ms report | col &gt; report.txt</b></tt><tt class="userinput"><b>CTRL-Z</b></tt></pre></blockquote><p>and then issue any one of the following:</p><blockquote><pre class="code">5% <tt class="userinput"><b>bg</b></tt>5% <tt class="userinput"><b>bg %</b></tt>         <i class="lineannotation">Current job</i>5% <tt class="userinput"><b>bg %1</b></tt>        <i class="lineannotation">Job number 1</i>5% <tt class="userinput"><b>bg %nr</b></tt>       <i class="lineannotation">Match initial string nroff</i>5% <tt class="userinput"><b>% &amp;</b></tt></pre></blockquote></td></tr><tr><td valign="top"><a name="break">break</a></a></td><td><tt class="literal">break</tt><p><a name="IXT-5-123036" />Resume execution following the <tt class="literal">end</tt> command of the nearest enclosing <tt class="literal">while</tt> or <tt class="literal">foreach</tt>.</p></td></tr><tr><td valign="top"><a name="breaksw">breaksw</a></a></td><td><tt class="literal">breaksw</tt><p><a name="IXT-5-123037" />Breakfrom a <tt class="literal">switch</tt>;continue execution after the <tt class="literal">endsw</tt>.</p></td></tr><tr><td valign="top"><a name="case">case</a></a></td><td><p><tt class="literal">case</tt> <em class="replaceable"><tt>pattern </tt></em><tt class="literal">:</tt></p><p><a name="IXT-5-123038" />Identify a <em class="emphasis">pattern</em> in a <tt class="literal">switch</tt>.</p></td></tr><tr><td valign="top"><a name="cd">cd</a></a></td><td><tt class="literal">cd</tt> [<em class="replaceable"><tt>dir</tt></em>]<p><a name="IXT-5-123039" /><a name="IXT-5-123040" />Change working directory to <em class="emphasis">dir</em>; default is home directory ofuser.  If <em class="emphasis">dir</em> is a relative pathname but is not in the currentdirectory, the <tt class="literal">cdpath</tt> variable is searched.See <a href="ch05_03.htm#unut-ch-5-sect-3.4">Section 5.3.4</a> earlier in this chapter.</p></td></tr><tr><td valign="top"><a name="chdir">chdir</a></a></td><td><tt class="literal">chdir</tt> [<em class="replaceable"><tt>dir</tt></em>]<p><a name="IXT-5-123041" />Same as <tt class="literal">cd</tt>.  Useful if you are redefining <tt class="literal">cd</tt>as an alias.</p></td></tr><tr><td valign="top"><a name="continue">continue</a></a></td><td><tt class="literal">continue</tt><p><a name="IXT-5-123042" />Resume execution of nearest enclosing <tt class="literal">while</tt> or <tt class="literal">foreach</tt>.</p></td></tr><tr><td valign="top"><a name="default">default</a></a></td><td><tt class="literal">default:</tt><p><a name="IXT-5-123043" />Label the default case (typically last) in a <tt class="literal">switch</tt>.</p></td></tr><tr><td valign="top"><a name="dirs">dirs</a></a></td><td><tt class="literal">dirs</tt> [<tt class="literal">-l</tt>]<p><a name="IXT-5-123044" />Print the directory stack, showing the current directory first; use <tt class="literal">-l</tt> to expand the home directory symbol (<tt class="literal">~</tt>)to the actual directory name.See also <b class="emphasis-bold"><a href="#popd">popd</a></b> and <b class="emphasis-bold"><a href="#pushd">pushd</a></b>.</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>] <em class="replaceable"><tt>string</tt></em><p><a name="IXT-5-123045" />Write <em class="emphasis">string</em> to standard output; if <tt class="literal">-n</tt> isspecified, the output is not terminated by a newline.Unlike the Unix version(<tt class="literal">/bin/echo</tt>) and the Bourne shell version,the C shell's <tt class="literal">echo</tt> doesn't support escape characters.See also <b class="emphasis-bold"><a href="#echo">echo</a></b> in<a href="ch02_01.htm">Chapter 2</a> and<a href="ch04_01.htm">Chapter 4</a>.</p></td></tr><tr><td valign="top"><a name="end">end</a></a></td><td><tt class="literal">end</tt><p><a name="IXT-5-123046" />Reservedword that ends a <tt class="literal">foreach</tt> or <tt class="literal">while</tt> statement.</p></td></tr><tr><td valign="top"><a name="endif">endif</a></a></td><td><p><tt class="literal">endif</tt></p><p><a name="IXT-5-123047" />Reserved word that ends an <tt class="literal">if</tt> statement.</p></td></tr><tr><td valign="top"><a name="endsw">endsw</a></a></td><td><tt class="literal">endsw</tt><p><a name="IXT-5-123048" />Reserved word that ends a <tt class="literal">switch</tt> statement.</p></td></tr><tr><td valign="top"><a name="eval">eval</a></a></td><td><tt class="literal">eval</tt> <em class="replaceable"><tt>args</tt></em><p><a name="IXT-5-123049" />Typically, <tt class="literal">eval</tt> is used in shell scripts,and <em class="emphasis">args</em> 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 &#8220;double-scanning&#8221; 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.)A Bourne shell example can be found under <b class="emphasis-bold"><a href="ch04_09.htm#eval">eval</a></b> in <a href="ch04_01.htm">Chapter 4</a>.Other uses of <tt class="literal">eval</tt> are shown next.</p><h4 class="refsect2">Examples</h4><p>The following lines can be placed in the <tt class="literal">.login</tt> fileto set up terminal characteristics:</p><blockquote><pre class="code"><tt class="userinput"><b>set noglobeval `tset -s xterm`unset noglob</b></tt></pre></blockquote><p>The following commands show the effect of <tt class="literal">eval</tt>:</p><blockquote><pre class="code">% <tt class="userinput"><b>set b='$a'</b></tt>% <tt class="userinput"><b>set a=hello</b></tt>% <tt class="userinput"><b>echo $b</b></tt>         <i class="lineannotation">Read the command line once</i>$a

⌨️ 快捷键说明

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