📄 ch17_11.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 17] 17.11 Using -exec to Create Custom Tests </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:38: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="ch17_01.htm"TITLE="17. Finding Files with find"><LINKREL="prev"HREF="ch17_10.htm"TITLE="17.10 Running Commands on What You Find "><LINKREL="next"HREF="ch17_12.htm"TITLE="17.12 Finding Many Things with One Command "></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="ch17_10.htm"TITLE="17.10 Running Commands on What You Find "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 17.10 Running Commands on What You Find "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 17<BR>Finding Files with find</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch17_12.htm"TITLE="17.12 Finding Many Things with One Command "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 17.12 Finding Many Things with One Command "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1234">17.11 Using -exec to Create Custom Tests </A></H2><PCLASS="para">Here's something that willreally make your head spin. Remember that <EMCLASS="emphasis">-exec</EM> doesn'tnecessarily evaluate to "true"; it only evaluates to true if thecommand it executes returns a<SPANCLASS="link">zero exit status (<ACLASS="linkend"HREF="ch44_07.htm"TITLE="Exit Status of UNIX Processes ">44.7</A>)</SPAN>.You can use this toconstruct custom <EMCLASS="emphasis">find</EM> tests.</P><PCLASS="para">Assume that you want to list files that are "beautiful."You have written a program called <EMCLASS="emphasis">beauty</EM> that returnszero if a file is beautiful, and non-zero otherwise.(This program can be a<SPANCLASS="link">shell script (<ACLASS="linkend"HREF="ch44_11.htm"TITLE="Set Exit Status of a Shell (Script) ">44.11</A>)</SPAN>,a<SPANCLASS="link"><EMCLASS="emphasis">perl</EM> (<ACLASS="linkend"HREF="ch37_01.htm#UPT-ART-5560"TITLE="What We Do and Don't Tell You About Perl ">37.1</A>)</SPAN>script, an executable from a C program, or anything you like.)</P><PCLASS="para">Here's an example: </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find . -exec beauty {} \; -print</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">In this command, <EMCLASS="emphasis">-exec</EM> is just another <EMCLASS="emphasis">find</EM> operator.The only difference is that we care about its value; we're not assuming that it will always be "true." <EMCLASS="emphasis">find</EM> executes the <EMCLASS="emphasis">beauty</EM> command for every file. Then <EMCLASS="emphasis">-exec</EM> evaluatesto true when <EMCLASS="emphasis">find</EM> is looking at a "beautiful" program, causing<EMCLASS="emphasis">find</EM> to print the filename. (Excuse me, causing <EMCLASS="emphasis">find</EM> toevaluate the <EMCLASS="emphasis">-print</EM>. <CODECLASS="literal">:-)</CODE>)</P><PCLASS="para">Of course, this ability is capable of infinite variation.If you're interested in finding beautiful C code, you coulduse the command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find . -name "*.[ch]" -exec beauty {} \; -print</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">And so on. For performance reasons, it's a good idea to put the<EMCLASS="emphasis">-exec</EM> operator as close to the end as possible. This avoidsstarting processes unnecessarily; the <EMCLASS="emphasis">-exec</EM> command will executeonly when the previous operators evaluate to true.<ACLASS="indexterm"NAME="AUTOID-19014"></A></P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</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="ch17_10.htm"TITLE="17.10 Running Commands on What You Find "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 17.10 Running Commands on What You Find "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="ch17_12.htm"TITLE="17.12 Finding Many Things with One Command "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 17.12 Finding Many Things with One Command "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">17.10 Running Commands on What You Find </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">17.12 Finding Many Things with One Command </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 + -