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

📄 ch19_02.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 19] 19.2 Introduction to Shell Archives </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:39: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="ch19_01.htm"TITLE="19. Creating and Reading Archives"><LINKREL="prev"HREF="ch19_01.htm"TITLE="19.1 Packing Up and Moving "><LINKREL="next"HREF="ch19_03.htm"TITLE="19.3 unshar: Unarchive a Shell Archive "></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="ch19_01.htm"TITLE="19.1 Packing Up and Moving "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 19.1 Packing Up and Moving "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 19<BR>Creating and Reading Archives</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch19_03.htm"TITLE="19.3 unshar: Unarchive a Shell Archive "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 19.3 unshar: Unarchive a Shell Archive "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-3680">19.2 Introduction to Shell Archives </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-3680-IX-SHELL-ARCHIVES"></A><ACLASS="indexterm"NAME="AUTOID-21215"></A>A shell archive or <EMCLASS="emphasis">shar</EM> file is a single file that contains one ormore other files.Files are extracted from the archive with the standard UNIX<SPANCLASS="link">Bourne shell (<ACLASS="linkend"HREF="ch44_03.htm"TITLE="What's a Shell, Anyway? ">44.3</A>)</SPAN>.A shell archive usually doesn't let you save and restore complete directoryhierarchies like<SPANCLASS="link"><EMCLASS="emphasis">cpio</EM> (<ACLASS="linkend"HREF="ch19_09.htm"TITLE="A System V Tape Archiver: cpio ">19.9</A>)</SPAN>and<SPANCLASS="link"><EMCLASS="emphasis">tar</EM> (<ACLASS="linkend"HREF="ch19_05.htm"TITLE="Using tar to Create and Unpack Archives ">19.5</A>)</SPAN>do, butit is completely portable and, as a result, is usedextensively on Usenet,an international network with many UNIX systems.</P><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="shar">shar</A><BR></TH><TDVALIGN="TOP">In the Bourne shell,<ACLASS="indexterm"NAME="AUTOID-21224"></A><SPANCLASS="link">the operator <CODECLASS="literal">&lt;&lt;</CODE> (<ACLASS="linkend"HREF="ch08_18.htm"TITLE="Here Documents ">8.18</A>)</SPAN>meansto take the following lines, up to a specified string,as input to a command.<ACLASS="indexterm"NAME="AUTOID-21229"></A>(This is often called a <EMCLASS="emphasis">here document</EM>.)Using this syntax and the<SPANCLASS="link"><EMCLASS="emphasis">cat</EM> (<ACLASS="linkend"HREF="ch25_02.htm"TITLE="Four Ways to Skin a cat ">25.2</A>)</SPAN>and<SPANCLASS="link"><EMCLASS="emphasis">echo</EM> (<ACLASS="linkend"HREF="ch08_06.htm"TITLE="Output Command-Line Arguments ">8.6</A>)</SPAN>commands, you can write a simple shell archiver (<EMCLASS="emphasis">shar</EM>)like the one below.<ACLASS="indexterm"NAME="AUTOID-21237"></A>Incidentally, manysystems already have<EMCLASS="emphasis">shar</EM> programsin place; thereare severalfreely available versions,including the one on the CD-ROM.Just about any of them are likely to be more sophisticated than theversion shown here&nbsp;- but this version shows the essence of how they work:</TD></TR></TABLE><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist">&#13;<ACLASS="co"HREF="ch44_16.htm"TITLE="44.16 Handling Command-Line Arguments with a for Loop ">for</A> <ACLASS="co"HREF="ch08_18.htm"TITLE="8.18 Here Documents ">&lt;&lt;</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">#!/bin/shfor filedo    echo &quot;echo restoring $file&quot;    echo &quot;cat &gt; $file &lt;&lt; 'XxXxXxXxXx-EOF-XxXxXxXxXx'&quot;    cat $file    echo &quot;XxXxXxXxXx-EOF-XxXxXxXxXx&quot;done</PRE></TD></TR></TABLE></P><PCLASS="para">The string <CODECLASS="literal">XxXxXxXxXx-EOF-XxXxXxXxXx</CODE> isentirely arbitrary&nbsp;- it just needs to be a string thatwon't otherwise appear in the input and can be usedby the shell to recognize when the here document isfinished.</P><PCLASS="para">When you give <EMCLASS="emphasis">shar</EM> a list of filenames, it willstring those files together on standard output, separating them with thatarbitrary string and the commands to split them up again.Simply redirect thisoutput stream to a file to create the archive.Forexample, the command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>shar file1 file2 &gt; archive.shar</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">will produce a file called <EMCLASS="emphasis">archive.shar</EM> that containsthe following data:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist">&#13;<ACLASS="co"HREF="ch45_26.htm"TITLE="45.26 Quoted hereis Document Terminators: sh vs. csh ">'...'</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">echo restoring file1cat &gt; file1 &lt;&lt; 'XxXxXxXxXx-EOF-XxXxXxXxXx'   ...<EMCLASS="emphasis">Text of file1 will be stored here</EM>   ...XxXxXxXxXx-EOF-XxXxXxXxXxecho restoring file2cat &gt; file2 &lt;&lt; 'XxXxXxXxXx-EOF-XxXxXxXxXx'   ...<EMCLASS="emphasis">Text of file2 will be stored here</EM>   ...XxXxXxXxXx-EOF-XxXxXxXxXx</PRE></TD></TR></TABLE></P><PCLASS="para">When this archive is run through <EMCLASS="emphasis">sh</EM>, the commandsit contains will be executed.Each here document(the lines from each <CODECLASS="literal">cat</CODE> up to the next<CODECLASS="literal">XxXxXxXxXx-EOF-XxXxXxXxXx</CODE>) will be outputto a file:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>sh archive.shar</B></CODE>restoring file1restoring file2$ <CODECLASS="userinput"><B>ls</B></CODE>archive.sharfile1file2</PRE></BLOCKQUOTE></P><PCLASS="para">The<SPANCLASS="link"><EMCLASS="emphasis">unshar</EM> (<ACLASS="linkend"HREF="ch19_03.htm"TITLE="unshar: Unarchive a Shell Archive ">19.3</A>)</SPAN>program does essentially the same thing.</P><BLOCKQUOTECLASS="note"><PCLASS="para"><STRONG>NOTE:</STRONG> You should never blindly runa shell archive supplied by someone you don't knowpersonally.<ACLASS="indexterm"NAME="AUTOID-21272"></A>An unscrupulous prankster could easilyinclude a &quot;Trojan horse&quot; command (like <CODECLASS="literal">rm&nbsp;*</CODE>) inthe middle of a seemingly innocuous archive, andcause you a lot of trouble.An easy way to do this is by browsing through the archive with the searchcommand in a program like<SPANCLASS="link"><EMCLASS="emphasis">more</EM> (<ACLASS="linkend"HREF="ch25_03.htm"TITLE="Using more to Page Through Files ">25.3</A>)</SPAN>.Use the search command (in <EMCLASS="emphasis">more</EM>, the command is <CODECLASS="literal">/</CODE>)to find each end-of-file string (like <CODECLASS="literal">XxXxXxXxXx</CODE>);look carefully at the commands between it and the <CODECLASS="literal">cat</CODE> that starts the next file.Of course, if the files in the shell archive are programs themselves,you should also check them before they're executed.<ACLASS="indexterm"NAME="AUTOID-21281"></A></P></BLOCKQUOTE><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="ch19_01.htm"TITLE="19.1 Packing Up and Moving "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 19.1 Packing Up and Moving "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="ch19_03.htm"TITLE="19.3 unshar: Unarchive a Shell Archive "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 19.3 unshar: Unarchive a Shell Archive "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">19.1 Packing Up and Moving </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">19.3 unshar: Unarchive a Shell Archive </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 + -