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

📄 ch33_09.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 33] 33.9 patch: Generalized Updating of Files that Differ </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:46:56Z"><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="ch33_01.htm"TITLE="33. Batch Editing"><LINKREL="prev"HREF="ch33_08.htm"TITLE="33.8 Batch Editing Gotcha: Editors Bomb on Big Files "><LINKREL="next"HREF="ch33_10.htm"TITLE="33.10 Quick Globals from the Command Line with qsubst "></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="ch33_08.htm"TITLE="33.8 Batch Editing Gotcha: Editors Bomb on Big Files "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 33.8 Batch Editing Gotcha: Editors Bomb on Big Files "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 33<BR>Batch Editing</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch33_10.htm"TITLE="33.10 Quick Globals from the Command Line with qsubst "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 33.10 Quick Globals from the Command Line with qsubst "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0311">33.9 patch: Generalized Updating of Files that Differ </A></H2><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="patch">patch</A><BR></TH><TDVALIGN="TOP">Like all of Larry Wall's widely used programs (including <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>,<ACLASS="indexterm"NAME="AUTOID-36968"></A><ACLASS="indexterm"NAME="AUTOID-36971"></A>a software configuration script called <EMCLASS="emphasis">Configure</EM>,and the <EMCLASS="emphasis">rn</EM> news reader),<EMCLASS="emphasis">patch</EM> betrays a whimsicalintelligence of its own.Feed it any kind of <SPANCLASS="link"><EMCLASS="emphasis">diff</EM> listing (<ACLASS="linkend"HREF="ch28_01.htm#UPT-ART-2680"TITLE="Checking Differences with diff ">28.1</A>)</SPAN>(not just <SPANCLASS="link">an editing script produced with the <EMCLASS="emphasis">-e</EM> option (<ACLASS="linkend"HREF="ch28_09.htm"TITLE="ex Scripts Built by diff ">28.9</A>)</SPAN>).<EMCLASS="emphasis">patch</EM> figures out what it needs to do to apply the diff,and updates the file, supplying all the while a breezy commentaryon what it's doing:</TD></TR></TABLE><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>patch &amp;amp;lt; testfile.diff</B></CODE>Hmm...  Looks like a normal diff to me...File to patch: <CODECLASS="userinput"><B>testfile</B></CODE>Patching file testfile using Plan A...Hunk #1 succeeded at 2.done</PRE></BLOCKQUOTE></P><PCLASS="para">As Larry once noted, <EMCLASS="emphasis">patch</EM> has done an awful lot to &quot;change theculture of computing.&quot;Almost all free software is now updated bymeans of patches rather than complete new releases.<EMCLASS="emphasis">patch</EM> issmart enough to discard any leading or trailing garbage (such as mailheaders or signatures) so that a program source file can be updated by piping a mail message containing a diff listing between old and newversions directly to <EMCLASS="emphasis">patch</EM>.</P><PCLASS="para">Here are a few of the other things <EMCLASS="emphasis">patch</EM> is smart enough todo:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">Figure out the name of the file to be updated and do it withoutasking (usually only if the diff file is a <SPANCLASS="link">context diff (<ACLASS="linkend"HREF="ch28_03.htm"TITLE="Context diffs ">28.3</A>)</SPAN>producedwith the <EMCLASS="emphasis">-c</EM> option).  </P></LI><LICLASS="listitem"><PCLASS="para">Look for a suitable <SPANCLASS="link">SCCS or RCS (<ACLASS="linkend"HREF="ch20_12.htm"TITLE="Protecting Files with SCCS or RCS ">20.12</A>)</SPAN>file and check it out, if the filename itself can't be found.</P></LI><LICLASS="listitem"><PCLASS="para">Handle diff listings that don't quite match.This makesit possible for <EMCLASS="emphasis">patch</EM> to update a file that the recipient hasalready changed from the one that the diff was based on.</P></LI><LICLASS="listitem"><PCLASS="para">Save any pieces of the diff file that don't end up being used, in afile named by addingthe suffix <EMCLASS="emphasis">.rej</EM> (reject) to the name of the file beingpatched.</P></LI><LICLASS="listitem"><PCLASS="para">Back up the file that is being patched, appending thesuffix <EMCLASS="emphasis">.orig</EM> to the name of the file being patched.</P></LI><LICLASS="listitem"><PCLASS="para">Recognize that its input may actually apply to several files, andpatch each of them separately.So, for example, a whole directorymight be updated by a &quot;patch&quot; file that contained diff listings foreach of the files in the directory.(By the way, the <EMCLASS="emphasis">-d</EM> optionto <EMCLASS="emphasis">patch</EM> tells it to <EMCLASS="emphasis">cd</EM> to a specified directory beforestarting work.)</P></LI><LICLASS="listitem"><PCLASS="para">Recognize (or at least speculate) that a patch might have been createdincorrectly, with the old and new files swapped.Larry says: &quot;Yes, I'm afraid that does happenoccasionally, human nature being what it is.&quot;<EMCLASS="emphasis">patch</EM>'s <EMCLASS="emphasis">-R</EM>option will force <EMCLASS="emphasis">patch</EM> to reverse the sense of the patch; what'sreally amusing is to see <EMCLASS="emphasis">patch</EM> suggest that this might be thething to do, when things seem out of sync.</P></LI></UL><PCLASS="para">If you are a programmer, <EMCLASS="emphasis">patch</EM> is worth studying just to seehow much a program can do to anticipate errors, deal with fuzzyinput, and in general &quot;make the computer do the dirty work.&quot;But ifyou're a programmer, you doubtless already know about <EMCLASS="emphasis">patch</EM>....</P><PCLASS="para">One last note:<EMCLASS="emphasis">patch</EM> is so useful that it's been added to manyUNIX systems.Check to see if your system has it before installingit from the CD-ROM.[I've seen buggy or crippled versions from software vendors, though.The one on the disc is worth comparing to yours. <EMCLASS="emphasis">&amp;amp;ndash;JP</EM>]</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">TOR</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="ch33_08.htm"TITLE="33.8 Batch Editing Gotcha: Editors Bomb on Big Files "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 33.8 Batch Editing Gotcha: Editors Bomb on Big 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="ch33_10.htm"TITLE="33.10 Quick Globals from the Command Line with qsubst "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 33.10 Quick Globals from the Command Line with qsubst "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">33.8 Batch Editing Gotcha: Editors Bomb on Big 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">33.10 Quick Globals from the Command Line with qsubst </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 + -