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

📄 ch08_11.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 8] 8.11 Which One Will bash Use? </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly &amp; Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:34:25Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch08_01.htm"TITLE="8. How the Shell Interprets What You Type"><LINKREL="prev"HREF="ch08_10.htm"TITLE="8.10 eval: When You Need Another Chance "><LINKREL="next"HREF="ch08_12.htm"TITLE="8.12 Which One Will the C Shell Use? "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch08_10.htm"TITLE="8.10 eval: When You Need Another Chance "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 8.10 eval: When You Need Another Chance "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 8<BR>How the Shell Interprets What You Type</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch08_12.htm"TITLE="8.12 Which One Will the C Shell Use? "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 8.12 Which One Will the C Shell Use? "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-9916">8.11 Which One Will bash Use? </A></H2><PCLASS="para">Article<ACLASS="xref"HREF="ch08_05.htm"TITLE="Command-Line Evaluation ">8.5</A><ACLASS="indexterm"NAME="AUTOID-8149"></A>gives an general overview of what the C shell does as it evaluates acommand line.<EMCLASS="emphasis">bash</EM> does something similar.This article takes a closer look at how you can control one part of thosesteps:whether <EMCLASS="emphasis">bash</EM> will choose a shell function, a built-in command, oran external command.(If you're interested in a detailed and humorous look at the way thisis handled in the C shell, read article<ACLASS="xref"HREF="ch08_12.htm"TITLE="Which One Will the C Shell Use? ">8.12</A>.)</P><PCLASS="para">Let's say that you want to write shell functions named <EMCLASS="emphasis">cd</EM>,<EMCLASS="emphasis">pushd</EM>, and <EMCLASS="emphasis">popd</EM>.They will run the shell's built-in <EMCLASS="emphasis">cd</EM>, <EMCLASS="emphasis">pushd</EM>, or <EMCLASS="emphasis">popd</EM>command, respectively.Next they execute another shell function named <EMCLASS="emphasis">setvars</EM> to do somesetup in the new directory:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist">&#13;<ACLASS="co"HREF="ch44_15.htm"TITLE="44.15 Handling Command-Line Arguments in Shell Scripts ">&quot;$@&quot;</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">cd() {            pushd() {     popd() {    cd &quot;$@&quot;       pushd &quot;$@&quot;       popd &quot;$@&quot;    setvars       setvars         setvars}                 }             }</PRE></TD></TR></TABLE></P><PCLASS="para">But which <EMCLASS="emphasis">cd</EM> will <EMCLASS="emphasis">bash</EM> use when you type <EMCLASS="emphasis">cd</EM>: thebuilt-in <EMCLASS="emphasis">cd</EM> or your <EMCLASS="emphasis">cd</EM> function?(Same question for <EMCLASS="emphasis">pushd</EM> and <EMCLASS="emphasis">popd</EM>.)Worse, what if the <CODECLASS="literal">cd&nbsp;&lt;&quot;&gt;$@&lt;&quot;&gt;</CODE> command inside the function makes<EMCLASS="emphasis">bash</EM> call your <EMCLASS="emphasis">cd</EM> function again, and that starts an endless loop?Well, that actually <EMCLASS="emphasis">will</EM> start a loop&nbsp;- and you need to know how toprevent it.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-8179"></A><ACLASS="indexterm"NAME="AUTOID-8181"></A><ACLASS="indexterm"NAME="AUTOID-8184"></A>Typing <EMCLASS="emphasis">command</EM> before the name of a command disables shell functionlookup.<EMCLASS="emphasis">bash</EM> will only execute a built-in command or an external commandwith that name.So, you could keep the functions from re-executing themselves by definingthem this way:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">cd() {                    pushd() {                popd() {    command cd &quot;$@&quot;       command pushd &quot;$@&quot;       command popd &quot;$@&quot;    setvars               setvars                  setvars}                         }                        }</PRE></BLOCKQUOTE></P><PCLASS="para">In the same way, if you don't want to run your new <EMCLASS="emphasis">pushd</EM> functionfor some reason, here's how to use the built-in <EMCLASS="emphasis">pushd</EM> once:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">bash$ <CODECLASS="userinput"><B>command pushd </B></CODE><CODECLASS="replaceable"><I>somewhere</I></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">The <EMCLASS="emphasis">command</EM> command still allows <EMCLASS="emphasis">bash</EM> to run an externalcommand (from your<SPANCLASS="link"><EMCLASS="emphasis">PATH</EM> (<ACLASS="linkend"HREF="ch06_04.htm"TITLE="The PATH Environment Variable ">6.4</A>)</SPAN>)with the name you give.To force <EMCLASS="emphasis">bash</EM> to use a built-in command&nbsp;- but not a shell function<ACLASS="indexterm"NAME="AUTOID-8204"></A>or an external command&nbsp;- type <EMCLASS="emphasis">builtin</EM> before the command name.Although <EMCLASS="emphasis">bash</EM> will always choose a built-in command before anexternal command,you can specify the built-in <EMCLASS="emphasis">echo</EM> unambiguously with:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">builtin echo -n 'What next? '</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-8212"></A>What if you want the external <EMCLASS="emphasis">echo</EM> command?The easiest way is probably by typing its absolute pathname.For example, when I was revising article<ACLASS="xref"HREF="ch08_20.htm"TITLE="How Many Backslashes?">8.20</A>,I wanted to test the four (!) different external versions of <EMCLASS="emphasis">echo</EM> on aSystem V machine&nbsp;- and not get the built-in <EMCLASS="emphasis">bash</EM> version.So I typed commands like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">bash$ <CODECLASS="userinput"><B>/bin/echo hi \\ there</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-8223"></A>Finally, you can enable or disable specific built-in <EMCLASS="emphasis">bash</EM> commandswith the <EMCLASS="emphasis">enable</EM> command.Unlike <EMCLASS="emphasis">command</EM> and <EMCLASS="emphasis">builtin</EM>, the effect of <EMCLASS="emphasis">enable</EM> lastsuntil you exit the shell.The command <EMCLASS="emphasis">enable&nbsp;-n</EM> disables one or more built-in commands; give the command names as arguments.For example, in my experiments for article<ACLASS="xref"HREF="ch08_20.htm"TITLE="How Many Backslashes?">8.20</A>,I could have made sure that I'd get an external <EMCLASS="emphasis">echo</EM> every timeby typing this first command once:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">bash$ <CODECLASS="userinput"><B>enable -n echo</B></CODE>bash$ <CODECLASS="userinput"><B>type echo</B></CODE>echo is hashed (/bin/echo)</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-8238"></A>The <EMCLASS="emphasis">bash</EM> <EMCLASS="emphasis">type</EM> command confirms that I'll now be using theexternal <EMCLASS="emphasis">echo</EM>.You can re-enable a disabled built-in with <CODECLASS="literal">enable</CODE> <CODECLASS="replaceable"><I>command-name</I></CODE>.And <EMCLASS="emphasis">enable&nbsp;-a</EM> lists the status of all <EMCLASS="emphasis">bash</EM> built-ins.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch08_10.htm"TITLE="8.10 eval: When You Need Another Chance "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 8.10 eval: When You Need Another Chance "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch08_12.htm"TITLE="8.12 Which One Will the C Shell Use? "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 8.12 Which One Will the C Shell Use? "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">8.10 eval: When You Need Another Chance </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">8.12 Which One Will the C Shell Use? </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed &amp; awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>

⌨️ 快捷键说明

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