📄 ch08_12.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 8] 8.12 Which One Will the C Shell Use? </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:34:26Z"><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_11.htm"TITLE="8.11 Which One Will bash Use? "><LINKREL="next"HREF="ch08_13.htm"TITLE='8.13 Is It "2>&1 file" or "> file 2>&1"? Why?'></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_11.htm"TITLE="8.11 Which One Will bash Use? "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 8.11 Which One Will bash Use? "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_13.htm"TITLE='8.13 Is It "2>&1 file" or "> file 2>&1"? Why?'><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT='Next: 8.13 Is It "2>&1 file" or "> file 2>&1"? Why?'BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-6060">8.12 Which One Will the C Shell Use? </A></H2><PCLASS="para">[Article<ACLASS="xref"HREF="ch08_11.htm"TITLE="Which One Will bash Use? ">8.11</A><ACLASS="indexterm"NAME="AUTOID-8254"></A>shows how to control whether <ICLASS="filename">bash</I> uses a built-incommand, a shell function, or an external command.The way you do that in the C shell is a little, errr, different.<ACLASS="indexterm"NAME="AUTOID-8258"></A>Chris Torek explains why, for example, <CODECLASS="literal">\rm</CODE> disables an aliasfor <ICLASS="filename">rm</I> and <CODECLASS="literal">\cd</CODE> disables the built-in <ICLASS="filename">cd</I> command.He starts with a fairly complex explanation, then gives some practicalguidelines.At the end is a "review" that's easy to follow and fun too. -JP]</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-8265"></A><ACLASS="indexterm"NAME="AUTOID-8267"></A>The C shell first breaks each input line into a <EMCLASS="emphasis">word vector</EM>.It then matches against aliases.Since <CODECLASS="literal">\rm</CODE> does not match <CODECLASS="literal">rm</CODE>, any alias is ignored.Eventually the C shell fully applies any quoting(since an alias can include quotes, some of this work must be deferred;since an alias can include multiple words, more word vector work mustbe done as well; it all gets rather hairy).</P><PCLASS="para">The C shell implements quoting by setting the 8th bit (bit 7) of eachbyte of a quoted character.Since <CODECLASS="literal">'*'|0x80</CODE> [a character <EMCLASS="emphasis">OR</EM>ed with 80 hex a.k.a.10000000 binary-<EMCLASS="emphasis">JP</EM> ] is not the same character as <CODECLASS="literal">'*'</CODE>,this prevents file name expansion, further word breaking, and so on.</P><PCLASS="para">Eventually, the shell has a fully "parsed" line.It then compares <CODECLASS="literal">word[0]</CODE> [the first word on the commandline-<EMCLASS="emphasis">JP</EM> ] against all the built-ins.If there is a match, it runs the corresponding built-in command(and it is up to that command to expand any remaining words;for instance, <CODECLASS="literal">ls *</CODE> in a directory containingonly the file <EMCLASS="emphasis">-l</EM> produces a long listing, but <CODECLASS="literal">jobs *</CODE>produces a usage message).If not, the shell performs globbing [filename wildcard expansion-<EMCLASS="emphasis">JP</EM> ]on the current word list, producing a new word list, and then:</P><OLCLASS="orderedlist"><LICLASS="listitem"><PCLASS="para">strips the 8th bit of each byte of each word</P></LI><LICLASS="listitem"><PCLASS="para"><EMCLASS="emphasis">exec</EM>()s the resulting command.</P></LI></OL><PCLASS="para">This means that:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">\cd</PRE></BLOCKQUOTE></P><PCLASS="para">not only bypasses any alias, but also reaches the built-in scanner as:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">'c'|0x80, 'd', '\0'</PRE></BLOCKQUOTE></P><PCLASS="para">which does not match the built-in command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">'c', 'd', '\0'</PRE></BLOCKQUOTE></P><PCLASS="para">and so does not run the <EMCLASS="emphasis">cd</EM> builtin.It is later stripped and the shell looks for an external program called<EMCLASS="emphasis">cd</EM>.</P><PCLASS="para">If you want to avoid alias substitution, but not built-in matching,you can replace:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">\cd foo <EMCLASS="emphasis">or</EM> \rm foo</PRE></BLOCKQUOTE></P><PCLASS="para">with:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">''cd foo <EMCLASS="emphasis">or</EM> ""rm foo</PRE></BLOCKQUOTE></P><PCLASS="para">These do not match the aliases - during alias scanning theyhave quote pairs in front of them - but do match any builtinsince the quotes have by then been stripped (setting bit 7 ofall the characters contained between the two quotes, here none).</P><PCLASS="para">Incidentally, since alias expansion occurs early, you can dosome peculiar things with it:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>[</B></CODE>Missing ].% <CODECLASS="userinput"><B>alias [ echo foo</B></CODE>% <CODECLASS="userinput"><B>[</B></CODE>foo</PRE></BLOCKQUOTE></P><PCLASS="para">(alias expansion occurs before globbing)</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>unalias [</B></CODE>unalias: Missing ].</PRE></BLOCKQUOTE></P><PCLASS="para">(unalias globs its arguments!)</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>unalias \[</B></CODE>% <CODECLASS="userinput"><B>alias unalias echo foo</B></CODE>unalias: Too dangerous to alias that.</PRE></BLOCKQUOTE></P><PCLASS="para">(the C shell attempts caution...)</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>alias \unalias echo foo</B></CODE>% <CODECLASS="userinput"><B>alias</B></CODE>unalias (echo foo)% <CODECLASS="userinput"><B>unalias unalias</B></CODE>foo unalias</PRE></BLOCKQUOTE></P><PCLASS="para">(but fails!)</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>''unalias unalias</B></CODE>% <CODECLASS="userinput"><B>alias</B></CODE>%</PRE></BLOCKQUOTE></P><PCLASS="para">(Fortunately, there is an exit.)</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">CT</SPAN> <SPANCLASS="bibliomisc">on Usenet, 14 November 1990</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_11.htm"TITLE="8.11 Which One Will bash Use? "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 8.11 Which One Will bash Use? "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_13.htm"TITLE='8.13 Is It "2>&1 file" or "> file 2>&1"? Why?'><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT='Next: 8.13 Is It "2>&1 file" or "> file 2>&1"? Why?'BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">8.11 Which One Will bash Use? </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.13 Is It "2>&1 file" or "> file 2>&1"? Why?</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 & 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 + -