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

📄 ch08_20.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 8.19. Program: tctee (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:38:56Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-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="ch08_01.htm"TITLE="8. File Contents"><LINKREL="prev"HREF="ch08_19.htm"TITLE="8.18. Program: tailwtmp"><LINKREL="next"HREF="ch08_21.htm"TITLE="8.20. Program: laston"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch08_19.htm"TITLE="8.18. Program: tailwtmp"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 8.18. Program: tailwtmp"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch08_01.htm"TITLE="8. File Contents"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch08_21.htm"TITLE="8.20. Program: laston"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 8.20. Program: laston"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch08-12981">8.19. Program: tctee</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="ch08-idx-1000005181-0"></A><ACLASS="indexterm"NAME="ch08-idx-1000005181-1"></A>Not all systems support the classic <EMCLASS="emphasis">tee</EM> program for splitting output pipes to multiple destinations. This command sends the output from <EMCLASS="emphasis">someprog</EM> to <EMCLASS="emphasis">/tmp/output</EM> and to the mail pipe beyond.</P><PRECLASS="programlisting">% someprog | tee /tmp/output | Mail -s 'check this' user@host.org</PRE><PCLASS="para">This program helps not only users who aren't on Unix systems and don't have a regular <EMCLASS="emphasis">tee</EM>. It also helps those who are, because it offers features not found on other version of <EMCLASS="emphasis">tee</EM>.</P><PCLASS="para">The four flag arguments are <BCLASS="emphasis.bold">-i</B> to ignore interrupts, <BCLASS="emphasis.bold">-a</B> to append to output files, <BCLASS="emphasis.bold">-u</B> for unbuffered output, and <BCLASS="emphasis.bold">-n</B> to omit copying the output on to standard out.</P><PCLASS="para">Because this program uses Perl's <ACLASS="indexterm"NAME="ch08-idx-1000005224-0"></A>magic open, you can specify pipes as well as files.</P><PRECLASS="programlisting">% someprog | tctee f1 &quot;|cat -n&quot; f2 &quot;&gt;&gt;f3&quot;</PRE><PCLASS="para">That sends the output from <EMCLASS="emphasis">someprog</EM> to the files <EMCLASS="emphasis">f1</EM> and <EMCLASS="emphasis">f2</EM>, appends it to <EMCLASS="emphasis">f3</EM>, sends a copy to the program <EMCLASS="emphasis">cat -n</EM>, and also produces the stream on standard output.</P><PCLASS="para">The program in <ACLASS="xref"HREF="ch08_20.htm#ch08-41455"TITLE="tctee">Example 8.8</A> is one of many venerable Perl programs written nearly a decade ago that still runs perfectly well. If written from scratch now, we'd probably <CODECLASS="literal">use</CODE> <CODECLASS="literal">strict</CODE>, warnings, and ten to thirty thousand lines of modules. But if it ain't broke . . .</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch08-41455">Example 8.8: tctee</A></H4><PRECLASS="programlisting">#!/usr/bin/perl# tctee - clone that groks process tees # perl3 compatible, or better.while ($ARGV[0] =~ /^-(.+)/ &amp;&amp; (shift, ($_ = $1), 1)) {    next if /^$/;    s/i// &amp;&amp; (++$ignore_ints, redo);    s/a// &amp;&amp; (++$append,      redo);    s/u// &amp;&amp; (++$unbuffer,    redo);    s/n// &amp;&amp; (++$nostdout,    redo);    die &quot;usage tee [-aiun] [filenames] ...\n&quot;;}if ($ignore_ints) {    for $sig ('INT', 'TERM', 'HUP', 'QUIT') { $SIG{$sig} = 'IGNORE'; }}$SIG{'PIPE'} = 'PLUMBER';$mode = $append ? '&gt;&gt;' : '&gt;';$fh = 'FH000';unless ($nostdout) {    %fh = ('STDOUT', 'standard output'); # always go to stdout    }$| = 1 if $unbuffer;for (@ARGV) {    if (!open($fh, (/^[^&gt;|]/ &amp;&amp; $mode) . $_)) {        warn &quot;$0: cannot open $_: $!\n&quot;; # like sun's; i prefer die        $status++;        next;    }    select((select($fh), $| = 1)[0]) if $unbuffer;    $fh{$fh++} = $_;}while (&lt;STDIN&gt;) {    for $fh (keys %fh) {        print $fh $_;    }}for $fh (keys %fh) {    next if close($fh) || !defined $fh{$fh};    warn &quot;$0: couldnt close $fh{$fh}: $!\n&quot;;    $status++;}exit $status;sub PLUMBER {    warn &quot;$0: pipe to \&quot;$fh{$fh}\&quot; broke!\n&quot;;    $status++;    delete $fh{$fh};}<ACLASS="indexterm"NAME="ch08-idx-1000004732-0"></A><ACLASS="indexterm"NAME="ch08-idx-1000004732-1"></A></PRE></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch08_19.htm"TITLE="8.18. Program: tailwtmp"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 8.18. Program: tailwtmp"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch08_21.htm"TITLE="8.20. Program: laston"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 8.20. Program: laston"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">8.18. Program: tailwtmp</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">8.20. Program: laston</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -