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

📄 ch24_11.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 24] 24.11 Edit Compressed Files with zvi, zex, and zed </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:41:39Z"><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="ch24_01.htm"TITLE="24. Other Ways to Get Disk Space"><LINKREL="prev"HREF="ch24_10.htm"TITLE="24.10 zloop: Run a Command on Compressed Files "><LINKREL="next"HREF="ch24_12.htm"TITLE="24.12 Compressing a Directory Tree: Fine-Tuning "></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="ch24_10.htm"TITLE="24.10 zloop: Run a Command on Compressed Files "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 24.10 zloop: Run a Command on Compressed Files "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 24<BR>Other Ways to Get Disk Space</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch24_12.htm"TITLE="24.12 Compressing a Directory Tree: Fine-Tuning "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 24.12 Compressing a Directory Tree: Fine-Tuning "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7180">24.11 Edit Compressed Files with zvi, zex, and zed </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-26720"></A><ACLASS="indexterm"NAME="AUTOID-26722"></A><ACLASS="indexterm"NAME="AUTOID-26724"></A><ACLASS="indexterm"NAME="AUTOID-26726"></A><ACLASS="indexterm"NAME="AUTOID-26729"></A><SPANCLASS="link">Compressed files (<ACLASS="linkend"HREF="ch24_07.htm"TITLE="Compressing Files to Save Space ">24.7</A>)</SPAN>save disk space.But compressed files aren't as convenient to work with: you have touncompress them before you can read or edit them.This script makes editing easier.It uncompresses files that were compressed with the GNU <EMCLASS="emphasis">gzip</EM> utility.Then it starts a text editor: <EMCLASS="emphasis">vi</EM>, <EMCLASS="emphasis">ex</EM>, or <EMCLASS="emphasis">ed</EM>.(It's easy to modify this to use other editors.)The <EMCLASS="emphasis">vi</EM> and <EMCLASS="emphasis">ex</EM> editors can<SPANCLASS="link">edit several files (<ACLASS="linkend"HREF="ch30_04.htm"TITLE="Editing Multiple Files with vi ">30.4</A>)</SPAN>;this script handles that.After you edit all the files, the script recompresses them in thebackground so that you don't have to wait.There's one more bit of trickery here: instead of uncompressing all filesyou specify before it starts the editor, the script uncompresses just thefirst file&nbsp;- itdoes the rest in the background while you're editing the first file.(It figures out what all the uncompressed files will be named.By the time the editor gets to them, they should have been uncompressed.)</P><PCLASS="para">This makes it easy to save a lot of disk space by keeping your files<EMCLASS="emphasis">gzip</EM>ped most of the time.The response is almost as fast as editing an uncompressed file,especially if the first file on the command line is a small one.Here's an example.I'll edit the files <EMCLASS="emphasis">qlog.gz</EM> and <EMCLASS="emphasis">/usr/central/data.gz</EM>with <EMCLASS="emphasis">vi</EM>.Next, I'll run <EMCLASS="emphasis">zed</EM> <SPANCLASS="link">editing script (<ACLASS="linkend"HREF="ch28_09.htm"TITLE="ex Scripts Built by diff ">28.9</A>)</SPAN>on <EMCLASS="emphasis">bigfile.gz</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>zvi qlog.gz /usr/central/data</B></CODE>   <EMCLASS="emphasis">The .gz isn't required</EM>    ...<EMCLASS="emphasis">Edit the two files like a normal vi multiple-file session</EM>...zvi: re-gzipping qlog /usr/central/data in the background...% <CODECLASS="userinput"><B>zed bigfile &lt; edscr</B></CODE>173571   <EMCLASS="emphasis">ed's file size counts</EM>183079zed: re-gzipping bigfile in the background...</PRE></BLOCKQUOTE></P><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="zvi">zvi</A><BR></TH><TDVALIGN="TOP">If there are any errors, the program prompts you when it can&nbsp;- otherwiseyou'll get<SPANCLASS="link">email (<ACLASS="linkend"HREF="ch01_33.htm"TITLE="UNIX Networking and Communications ">1.33</A>)</SPAN>with a copy of the error messages.Please test this script carefully on your system before you use it.All this trickery could need a little tweaking to work right.&#13;</TD></TR></TABLE><PCLASS="para">Most of the script is pretty straightforward.Unfortunately, the script won't work with editors like<SPANCLASS="link"><EMCLASS="emphasis">Emacs</EM> (<ACLASS="linkend"HREF="ch32_01.htm#UPT-ART-5540"TITLE="Emacs: The Other Editor ">32.1</A>)</SPAN>that try to open all files from the command line immediately.You could change that by making the script uncompress all filesbefore starting Emacs.</P><PCLASS="para">The section that follows is interesting.It's the part that uncompresses background files.If there's an error in the background, how does the script catch it?</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">test -n &quot;$bgfiles&quot; &amp;&amp; $uncompress $bgfiles &gt;$t 2&gt;&amp;1 &amp;$prog $filesif [ -s $t ]then    echo &quot;$myname: 'gunzip $bgfiles' bombed:&quot; 1&gt;&amp;2    cat $t 1&gt;&amp;2    $echo &quot;Should I try to gzip all files [ny](n)? $nnl&quot;    read ans</PRE></BLOCKQUOTE></P><PCLASS="para">The standard output and standard error of the background job goes to atemporary file, <CODECLASS="literal">$t</CODE>.The editor (<CODECLASS="literal">$prog</CODE>) runs.After you quit the editor, the<SPANCLASS="link"><EMCLASS="emphasis">test</EM> (<CODECLASS="literal">[</CODE>) (<ACLASS="linkend"HREF="ch44_20.htm"TITLE="test: Testing Files and Strings ">44.20</A>)</SPAN><EMCLASS="emphasis">-s</EM> optionchecks the temporary file.If the file isn't empty, the script shows you the errors from it(the file) and asks you whether the files should be re<EMCLASS="emphasis">gzip</EM>ped.</P><PCLASS="para">The script is written to have two other<SPANCLASS="link">links (<ACLASS="linkend"HREF="ch18_03.htm"TITLE="Files with Two or More Names">18.3</A>)</SPAN>.You may want to make more or fewer links though, depending on the editors your system has.If you install the script from the CD-ROM, the links will bemade for you.If you type in the script, put it in an executable file named <EMCLASS="emphasis">zvi</EM>.Then make the links:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>chmod 755 zvi</B></CODE>% <CODECLASS="userinput"><B>ln zvi zex</B></CODE>% <CODECLASS="userinput"><B>ln zvi zed</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">The script tests the name it was called with, from <CODECLASS="literal">$0</CODE>, to decidewhich editor to use.This trick saves disk space.You can change the editors it uses by modifying the script and adding orremoving links.</P><PCLASS="para">The absolute pathnames at the start of the script may need to be changedfor your system.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</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="ch24_10.htm"TITLE="24.10 zloop: Run a Command on Compressed Files "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 24.10 zloop: Run a Command on Compressed 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="ch24_12.htm"TITLE="24.12 Compressing a Directory Tree: Fine-Tuning "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 24.12 Compressing a Directory Tree: Fine-Tuning "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">24.10 zloop: Run a Command on Compressed 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">24.12 Compressing a Directory Tree: Fine-Tuning </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 + -