📄 ch01_18.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 1] 1.18 Who Handles Wildcards? </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:31:22Z"><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="ch01_01.htm"TITLE="1. Introduction"><LINKREL="prev"HREF="ch01_17.htm"TITLE="1.17 Filename Extensions "><LINKREL="next"HREF="ch01_19.htm"TITLE="1.19 The Tree Structure of the Filesystem "></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="ch01_17.htm"TITLE="1.17 Filename Extensions "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 1.17 Filename Extensions "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 1<BR>Introduction</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch01_19.htm"TITLE="1.19 The Tree Structure of the Filesystem "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 1.19 The Tree Structure of the Filesystem "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1013">1.18 Who Handles Wildcards? </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-1782"></A><SPANCLASS="link">Wildcards (<ACLASS="linkend"HREF="ch01_16.htm"TITLE="Wildcards ">1.16</A>)</SPAN>are actually defined by the UNIX shells, rather than theUNIX filesystem. In theory, a new shell could define newwildcards, and consequently, we should discuss wildcarding when wediscuss the shell. In practice, all UNIX shells (including<SPANCLASS="link"><EMCLASS="emphasis">ksh</EM>, <EMCLASS="emphasis">bash</EM>, and other variants (<ACLASS="linkend"HREF="ch01_08.htm"TITLE="There Are Many Shells ">1.8</A>)</SPAN>)honor the same wildcard conventions, and we don't expect to see anyonechange the rules. (But different shells do different things<SPANCLASS="link">when a wildcard doesn't match (<ACLASS="linkend"HREF="ch15_04.htm"TITLE="What if a Wildcard Doesn't Match? ">15.4</A>)</SPAN>.)</P><PCLASS="para">You may see different wildcarding if you buy a special-purposeshell that emulates another operating system (for example, a shellthat looks like DEC's DCL)-in this case, your shell will obey theother operating system's wildcard rules. But even in this case,operating system designers stick to a reasonably similar set ofwildcard rules.</P><PCLASS="para">The fact that the shell defines wildcards, rather than the filesystemitself or the program you're running, has some important implicationsfor a few commands.Most of the time, a program never sees wildcards.For example, typing:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>lpr *</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">is exactly the same as typing:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>lpr </B></CODE><CODECLASS="replaceable"><I>file1 file2 file3 file4 file5</I></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">In this case everything works as expected.But there are other situations in whichwildcards don't work at all.Assume you want to read some files from a tape, which requiresthe command<SPANCLASS="link"><EMCLASS="emphasis">tar x</EM> (<ACLASS="linkend"HREF="ch20_04.htm"TITLE="Restoring Files from Tape with tar ">20.4</A>)</SPAN>,so you type the command<CODECLASS="literal">tar x *.txt</CODE>.Will you be happy or disappointed?</P><PCLASS="para">You'll be disappointed - unless older versions of the files you wantare already in your<SPANCLASS="link">current directory (<ACLASS="linkend"HREF="ch01_21.htm"TITLE="Making Pathnames ">1.21</A>)</SPAN>.The shell expands the wildcard<CODECLASS="literal">*.txt</CODE>, according to what's in the current directory,<EMCLASS="emphasis">before it hands the completed command line over to tar forexecution</EM>. All<EMCLASS="emphasis">tar</EM> gets is a list of files. But you're probably not interestedin the current directory; you probably want the wildcard <CODECLASS="literal">*</CODE> tobe expanded on the tape, retrieving any <CODECLASS="literal">*.txt</CODE> files that thetape has.</P><PCLASS="para">There's a way to pass wildcards to programs, without having them interpretedby the shell.Simply put <CODECLASS="literal">*.txt</CODE> in<SPANCLASS="link">quotes (<ACLASS="linkend"HREF="ch08_14.htm"TITLE="Bourne Shell Quoting ">8.14</A>)</SPAN>.<ACLASS="indexterm"NAME="AUTOID-1814"></A><ACLASS="indexterm"NAME="AUTOID-1817"></A>The quotes prevent the UNIX shell from expanding the wildcard, passingit to the command unchanged. Programs that can beused in this way (like<SPANCLASS="link"><EMCLASS="emphasis">uucp</EM> and <EMCLASS="emphasis">rcp</EM> (<ACLASS="linkend"HREF="ch01_33.htm"TITLE="UNIX Networking and Communications ">1.33</A>)</SPAN>)know how to handlewildcards, obeying the same rules as the shell (in fact, these programsusually start a shell to interpret their arguments). You only need tomake sure that the programs see the wildcards, that they aren't stripped bythe shell before it passes the command line to the program.As a more generalrule, you should be aware of when and why a wildcard gets expanded,and you should know how to make sure that wildcards are expanded at anappropriate time.</P><BLOCKQUOTECLASS="note"><PCLASS="para"><STRONG>NOTE:</STRONG> <ACLASS="indexterm"NAME="AUTOID-1825"></A><ACLASS="indexterm"NAME="AUTOID-1828"></A>If your shell understands the<SPANCLASS="link"><CODECLASS="literal">{}</CODE> characters (<ACLASS="linkend"HREF="ch09_05.htm"TITLE="Build Strings with { } ">9.5</A>)</SPAN>,you can use them because they can generate any string - not just filenamesthat already exist.You have to type the unique part of each name, but you only have totype the common part once.For example, to extract the files called <EMCLASS="emphasis">project/wk9/summary</EM>,<EMCLASS="emphasis">project/wk14/summary</EM>, and <EMCLASS="emphasis">project/wk15/summary</EM> from a<EMCLASS="emphasis">tar</EM> tape, you might use:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>tar xv project/wk{9,14,15}/summary</B></CODE>x project/wk9/summary, 3161 bytes, 7 tape blocksx project/wk14/summary, 878 bytes, 2 tape blocksx project/wk15/summary, 2268 bytes, 5 tape blocks</PRE></BLOCKQUOTE></P></BLOCKQUOTE><PCLASS="para">Some versions of <EMCLASS="emphasis">tar</EM> understand wildcards, but many don't. There is a<SPANCLASS="link">clever workaround (<ACLASS="linkend"HREF="ch20_09.htm"TITLE="When a Program Doesn't Understand Wildcards ">20.9</A>)</SPAN>.</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="ch01_17.htm"TITLE="1.17 Filename Extensions "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 1.17 Filename Extensions "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="ch01_19.htm"TITLE="1.19 The Tree Structure of the Filesystem "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 1.19 The Tree Structure of the Filesystem "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">1.17 Filename Extensions </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">1.19 The Tree Structure of the Filesystem </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 + -