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

📄 ch08_09.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 8] 8.9 Wildcards Inside of Aliases </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:23Z"><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_08.htm"TITLE="8.8 A Directory for Commands You Shouldn't Run "><LINKREL="next"HREF="ch08_10.htm"TITLE="8.10 eval: When You Need Another Chance "></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_08.htm"TITLE="8.8 A Directory for Commands You Shouldn't Run "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 8.8 A Directory for Commands You Shouldn't Run "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_10.htm"TITLE="8.10 eval: When You Need Another Chance "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 8.10 eval: When You Need Another Chance "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1852">8.9 Wildcards Inside of Aliases </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-8052"></A><ACLASS="indexterm"NAME="AUTOID-8055"></A><ACLASS="indexterm"NAME="AUTOID-8058"></A>Here's another example in which command-line parsing is important.Consider this alias for counting the number of words in all files:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch29_06.htm"TITLE="29.6 Counting Lines, Words, and Characters: wc ">wc</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">% <CODECLASS="userinput"><B>alias words &quot;wc -w *&quot;</B></CODE></PRE></TD></TR></TABLE></P><PCLASS="para">Right away, we can see one effect of command-line parsing. The shellsees the quotation marks, and knows not to expand wildcards inside thequotation marks.Therefore, <CODECLASS="literal">words</CODE> is aliased to <CODECLASS="literal">wc -w *</CODE>; the <CODECLASS="literal">*</CODE>isn't evaluated when you create the alias.(If wildcards were processed before quotes, this won't work.)</P><PCLASS="para">Now, think about what happens when you execute the alias. You type:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>words</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">&#13;The shell starts working through<SPANCLASS="link">its steps (<ACLASS="linkend"HREF="ch08_05.htm"TITLE="Command-Line Evaluation ">8.5</A>)</SPAN>,and eventually performsalias substitution. When this happens, it converts your command into:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">wc -w *</PRE></BLOCKQUOTE></P><PCLASS="para">Now, watch carefully. The shell continues working through the processof interpretation (redirection, variable substitution, commandsubstitution), and eventually gets to filename expansion. At thispoint, the shell sees the <CODECLASS="literal">*</CODE> on the command line, expands it, andsubstitutes the files in the current directory. Seems simple enough.But think:you didn't type this <CODECLASS="literal">*</CODE>; the shell put it there when it expanded the wildcard.What would have happened if the shell expanded wildcards beforesubstituting aliases? The <CODECLASS="literal">*</CODE> would never have been expanded; by thetime the shell put it on the command line, the wildcard expansionstage would be over, and you'd just count the words in a file named <CODECLASS="literal">*</CODE>(which probably doesn't exist).</P><PCLASS="para">To me, the amazing thing is that all this works&nbsp;- and works well! Theworkings of the command line are intricate and complex, but the shell almostalways does what you want&nbsp;- and without a lot of thought.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <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_08.htm"TITLE="8.8 A Directory for Commands You Shouldn't Run "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 8.8 A Directory for Commands You Shouldn't Run "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_10.htm"TITLE="8.10 eval: When You Need Another Chance "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 8.10 eval: When You Need Another Chance "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">8.8 A Directory for Commands You Shouldn't Run </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.10 eval: When You Need Another Chance </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 + -