📄 ch08_05.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 8] 8.5 Command-Line Evaluation </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:11Z"><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_04.htm"TITLE="8.4 Command Evaluation and Accidentally Overwriting Files"><LINKREL="next"HREF="ch08_06.htm"TITLE="8.6 Output Command-Line Arguments "></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_04.htm"TITLE="8.4 Command Evaluation and Accidentally Overwriting Files"><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 8.4 Command Evaluation and Accidentally Overwriting Files"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_06.htm"TITLE="8.6 Output Command-Line Arguments "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 8.6 Output Command-Line Arguments "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1850">8.5 Command-Line Evaluation </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-7596"></A><ACLASS="indexterm"NAME="AUTOID-7599"></A>With all the different substitution mechanisms available in theC shell, it's important to know which take precedence.Here's the order in which the C shell interprets the command line:</P><OLCLASS="orderedlist"><LICLASS="listitem"><PCLASS="para">History substitution</P></LI><LICLASS="listitem"><PCLASS="para">Splitting words (including special characters)</P></LI><LICLASS="listitem"><PCLASS="para">Updating the history list</P></LI><LICLASS="listitem"><PCLASS="para">Interpreting single quotes (<CODECLASS="literal">'</CODE>) and double quotes (<CODECLASS="literal">"</CODE>)</P></LI><LICLASS="listitem"><PCLASS="para">Alias substitution</P></LI><LICLASS="listitem"><PCLASS="para">Redirection of input and output ( e.g., <CODECLASS="literal">></CODE>, <CODECLASS="literal"><</CODE>, and <CODECLASS="literal">|</CODE>)</P></LI><LICLASS="listitem"><PCLASS="para">Variable substitution</P></LI><LICLASS="listitem"><PCLASS="para">Command substitution</P></LI><LICLASS="listitem"><PCLASS="para">Filename expansion</P></LI></OL><PCLASS="para">(The Bourne shell is essentially the same, except that it doesn'tperform history substitution or alias substitution.)</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-7628"></A>History substitutions are always done first. That'swhy quotes won't protect a <CODECLASS="literal">!</CODE> from the shell; the shell sees theexclamation point and substitutes a command from the history beforeit's even thought about the quotation marks.To prevent history substitution, you need to<ACLASS="indexterm"NAME="AUTOID-7631"></A><SPANCLASS="link">use a backslash (<ACLASS="linkend"HREF="ch08_15.htm"TITLE="Differences Between Bourne and C Shell Quoting ">8.15</A>)</SPAN>.</P><PCLASS="para">Let's work through a simple command line that uses several of thesefeatures.Nothing in this command line will be difficult, but it willgive you a feeling for what we mean by saying that "the shell performsvariable substitution after alias substitution."Here's the command line; it has both space and TAB characters:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>ls -l $HOME/* | grep "Mar 7"</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">And here's what happens:</P><OLCLASS="orderedlist"><LICLASS="listitem"><PCLASS="para">There are no history operators, so<SPANCLASS="link">history substitution (<ACLASS="linkend"HREF="ch11_02.htm"TITLE="History in a Nutshell ">11.2</A>)</SPAN>doesn't happen.(The Bourne shell wouldn't perform this step.)</P></LI><LICLASS="listitem"><PCLASS="para">The command line is split into separate "words" at the whitespace characters.The words are <CODECLASS="literal">ls</CODE>, <CODECLASS="literal">-l</CODE>,<CODECLASS="literal">$HOME/*</CODE>, <CODECLASS="literal">|</CODE>, <CODECLASS="literal">grep</CODE>, and <CODECLASS="literal">"Mar 7"</CODE>.The shell ignores the amount of whitespace (spaces and TABs)between different wordsin a command line.Any unquoted whitespace creates a new word.The shell doesn't do anything special with options (like <CODECLASS="literal">-l</CODE>).Options are passed to the command being run, just like any other word;[2]the command decides how to interpret them.Also, notethat<SPANCLASS="link">quotes (<ACLASS="linkend"HREF="ch08_14.htm"TITLE="Bourne Shell Quoting ">8.14</A>)</SPAN>prevent the shell from splitting <CODECLASS="literal">"Mar 7"</CODE> into two wordsor eating the two spaces - even though quote interpretation comes later.[3]At this point, thecommand line looks like this:</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[2] The convention of starting options with a dash (<CODECLASS="literal">-</CODE>) is justthat: a convention.Although option handling is being<SPANCLASS="link">standardized (<ACLASS="linkend"HREF="ch44_18.htm"TITLE="Standard Command-Line Parsing ">44.18</A>)</SPAN>,each command can interpret its options any way it wants to.</P><PCLASS="para">[3] In an <EMCLASS="emphasis">ls -l</EM> listing, dates less than 10 have two spaces beforethem (they're printed in a field 2 characters wide).</P></BLOCKQUOTE><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">ls -l $HOME/* | grep "Mar 7"</PRE></BLOCKQUOTE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para">The shell sticks the command line onto the history list.The Bourne shellwouldn't perform this step, either.</P></LI><LICLASS="listitem"><PCLASS="para">The shell recognizes the double quotes around <CODECLASS="literal">"Mar 7"</CODE> and notes thatwildcard expansion (yet to come) shouldn't take place inside the quotes.</P></LI><LICLASS="listitem"><PCLASS="para">The shell checks whether or not <CODECLASS="literal">ls</CODE> or <CODECLASS="literal">grep</CODE> are<SPANCLASS="link">aliases (<ACLASS="linkend"HREF="ch10_02.htm"TITLE="Aliases for Common Commands ">10.2</A>)</SPAN>.They could be, but we're assuming they aren't.</P></LI><LICLASS="listitem"><PCLASS="para">The shell notices the <CODECLASS="literal">|</CODE>, and does<SPANCLASS="link">whatever's required (<ACLASS="linkend"HREF="ch13_01.htm#UPT-ART-1023"TITLE="Using Standard Input and Output">13.1</A>)</SPAN>to set up apipeline.</P></LI><LICLASS="listitem"><PCLASS="para">The shell notices the<SPANCLASS="link">environment variable (<ACLASS="linkend"HREF="ch06_01.htm#UPT-ART-1170"TITLE="What Environment Variables Are Good For ">6.1</A>)</SPAN><CODECLASS="literal">$HOME</CODE>,and replaces thisvariable with its value (<CODECLASS="literal">/home/mikel</CODE>).At this point, thecommand line looks like:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">ls -l /home/mikel/* | grep "Mar 7"</PRE></BLOCKQUOTE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para">The shell looks for<SPANCLASS="link">backquotes (<ACLASS="linkend"HREF="ch09_16.htm"TITLE="Command Substitution ">9.16</A>)</SPAN>,executes any command inside thebackquotes, and inserts its output on the command line. In this case,there's nothing to do.(If there are wildcards or variables inside the backquotes, they aren'tinterpreted before the shell runs the command inside the backquotes.)</P></LI><LICLASS="listitem"><PCLASS="para">The shell looks for<SPANCLASS="link">wildcards (<ACLASS="linkend"HREF="ch01_16.htm"TITLE="Wildcards ">1.16</A>)</SPAN>.In this case, it sees the <CODECLASS="literal">*</CODE> andexpands the filename accordingly, leaving something like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">ls -l /home/mikel/ax ... /home/mikel/zip | grep "Mar 7"</PRE></BLOCKQUOTE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para">The shell executes the <EMCLASS="emphasis">ls</EM> command, executes the <EMCLASS="emphasis">grep</EM>command, with the aforementioned pipe sending the <EMCLASS="emphasis">ls</EM> outputinto <EMCLASS="emphasis">grep</EM>'s input.</P></LI></OL><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-7704"></A><ACLASS="indexterm"NAME="AUTOID-7707"></A>One character you'll see often on command lines is <CODECLASS="literal">;</CODE>(semicolon).It's used as a command separator:type one complete command line - then, instead of pressing RETURN, typea semicolon and another complete command line.Chaining commands with semicolons is especially useful in<SPANCLASS="link">subshells (<ACLASS="linkend"HREF="ch13_07.htm"TITLE="The () Subshell Operators ">13.7</A>)</SPAN>,aliases,and<SPANCLASS="link">lists (<ACLASS="linkend"HREF="ch13_08.htm"TITLE="Using {list} to Group Bourne Shell Commands ">13.8</A>)</SPAN>-this book has lots of examples - in articles<ACLASS="xref"HREF="ch40_02.htm"TITLE="Waiting a Little While: sleep ">40.2</A>and<ACLASS="xref"HREF="ch10_02.htm"TITLE="Aliases for Common Commands ">10.2</A>,for instance.There's more about command-line interpretation in the articles on<SPANCLASS="link">wildcards inside aliases (<ACLASS="linkend"HREF="ch08_09.htm"TITLE="Wildcards Inside of Aliases ">8.9</A>)</SPAN>,<SPANCLASS="link"><EMCLASS="emphasis">eval</EM> (<ACLASS="linkend"HREF="ch08_10.htm"TITLE="eval: When You Need Another Chance ">8.10</A>)</SPAN>,<SPANCLASS="link">conditional execution (<ACLASS="linkend"HREF="ch44_09.htm"TITLE="Testing Your Success ">44.9</A>)</SPAN>,and many others.[For some nitty-gritty details about the C shell that are fun, too,I recommend Chris Torek's article<ACLASS="xref"HREF="ch08_12.htm"TITLE="Which One Will the C Shell Use? ">8.12</A>.-<EMCLASS="emphasis">JP</EM> ]</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">DG</SPAN>, <SPANCLASS="authorinitials">ML</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_04.htm"TITLE="8.4 Command Evaluation and Accidentally Overwriting Files"><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 8.4 Command Evaluation and Accidentally Overwriting Files"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_06.htm"TITLE="8.6 Output Command-Line Arguments "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 8.6 Output Command-Line Arguments "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">8.4 Command Evaluation and Accidentally Overwriting Files</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.6 Output Command-Line Arguments </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 + -