📄 ch21_08.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 21] 21.8 The MAILCHECK and mail Variables Check More than Mail</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:40:08Z"><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="ch21_01.htm"TITLE="21. More About Managing Files"><LINKREL="prev"HREF="ch21_07.htm"TITLE="21.7 Setting File Modification Time with touch "><LINKREL="next"HREF="ch21_09.htm"TITLE="21.9 Keep File Printouts Up-to-Date Automatically with make"></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="ch21_07.htm"TITLE="21.7 Setting File Modification Time with touch "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 21.7 Setting File Modification Time with touch "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 21<BR>More About Managing Files</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch21_09.htm"TITLE="21.9 Keep File Printouts Up-to-Date Automatically with make"><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 21.9 Keep File Printouts Up-to-Date Automatically with make"BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1220">21.8 The MAILCHECK and mail Variables Check More than Mail</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-23296"></A>Depending on how your system is set up, you may notice that itperiodically says something like <CODECLASS="literal">You have new mail</CODE>.When you run your<SPANCLASS="link">mail program (<ACLASS="linkend"HREF="ch01_33.htm"TITLE="UNIX Networking and Communications ">1.33</A>)</SPAN>,the mail will be waiting for you in your mailbox.You can also use this feature to check for changesin several mailboxes, as well as changes in files and directories that don'thold mail - more about that in a minute.</P><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-1220-SECT-1.1">21.8.1 For C Shell Users </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-23304"></A>If you use the C shell, this feature is controlledby the <EMCLASS="emphasis">mail</EM> shell variable (usually set in your<SPANCLASS="link"><EMCLASS="emphasis">.cshrc</EM> file (<ACLASS="linkend"HREF="ch02_03.htm"TITLE="What Goes in Shell Setup Files? ">2.3</A>)</SPAN>).</P><PCLASS="para">The shell normally checks your mailbox every five minutes.However, you can set a different interval at the start ofthe list. For example, the command below tells the shell to checkmy mailbox every 60 seconds:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>set mail=(60 /usr/spool/mail/mikel)</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Note that the exact filename depends upon how your mail system isset up.For example, many systems use <EMCLASS="emphasis">/usr/mail</EM> instead of <EMCLASS="emphasis">/usr/spool/mail</EM>.Checking for mail takes time and can delay your prompt on busy systems.Don't set a short interval unless you need to.</P><DIVCLASS="sect3"><H4CLASS="sect3"><ACLASS="title"NAME="UPT-ART-1220-SECT-1.1.1">21.8.1.1 Multiple Mailboxes </A></H4><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-23320"></A><ACLASS="indexterm"NAME="AUTOID-23323"></A>Many users need to watch more than one mailbox. For example, I needto watch <EMCLASS="emphasis">/usr/spool/mail/mikel</EM>; but if I'm responsible forproduct support, and my company maintains a special mail ID forsupport questions, I might also want to watch<EMCLASS="emphasis">/usr/spool/mail/prodsupport</EM>. To do this, we set the <EMCLASS="emphasis">mail</EM>variable so that it's a list of important files and directories:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>set mail=(/usr/spool/mail/mikel /usr/spool/mail/prodsupport)</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"> When the list has more than one file, the shell will tell you which filehas changed with a message like <CODECLASS="literal">new mail in /usr/spool/mail/prodsupport</CODE>.</P></DIV><DIVCLASS="sect3"><H4CLASS="sect3"><ACLASS="title"NAME="UPT-ART-1220-SECT-1.1.2">21.8.1.2 Watching Other Files </A></H4><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-23337"></A>All <EMCLASS="emphasis">mail</EM> is doing is looking to see whether or not the file haschanged; it doesn't know that it's looking at a "mail" file.Therefore, you can use it to watch anything you want; your list can even include directories. For example, let's say thatyou're running a program that periodically writes to the file<EMCLASS="emphasis">/home/los/mikel/radio/log.out</EM>. Then you can set <EMCLASS="emphasis">mail</EM> asfollows:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>set mail=(/home/los/mikel/radio/log.out </B></CODE><CODECLASS="replaceable"><I>other-files</I></CODE><CODECLASS="userinput"><B>)</B></CODE></PRE></BLOCKQUOTE></P></DIV><DIVCLASS="sect3"><H4CLASS="sect3"><ACLASS="title"NAME="UPT-ART-1220-SECT-1.1.3">21.8.1.3 Watching Directories </A></H4><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-23351"></A>Watching a directory isthe same as watching a file; you'll be notified whenever the directorychanges (whenever a file isadded or deleted in the directory). So let's modify our previous exampleslightly; let's say that your reports are named<EMCLASS="emphasis">/home/los/mikel/radio/log/</EM><CODECLASS="replaceable"><I>date</I></CODE>, where the <EMCLASS="emphasis">date</EM>indicates when the report was created. Every report thus generates anew file. In this case, you'd want to watch the log directory for thecreation of new files.</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>set mail=(/home/los/mikel/radio/log </B></CODE><CODECLASS="replaceable"><I>other-files</I></CODE><CODECLASS="userinput"><B>)</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Here's another example. Let's say that you suspect someone is using<SPANCLASS="link">UUCP (<ACLASS="linkend"HREF="ch01_33.htm"TITLE="UNIX Networking and Communications ">1.33</A>)</SPAN>to send company secrets to a system named <EMCLASS="emphasis">somewhere</EM>.You want to watch this systems's UUCP traffic very carefully. To doso, you can tell the shell to inform you whenever the logfile changes:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>set mail=(5 /usr/spool/uucp/.Log/uucico/somewhere)</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">We've told the shell to check the log every five seconds because,given that we suspect security problems, we want to get our reportsimmediately.</P><PCLASS="para">If the directory you're watching is actually a<SPANCLASS="link">symbolic link (<ACLASS="linkend"HREF="ch18_04.htm"TITLE="More About Links ">18.4</A>)</SPAN>to anotherdirectory,be sure to check the actual directory and not the link.<ACLASS="indexterm"NAME="AUTOID-23371"></A><ACLASS="indexterm"NAME="AUTOID-23373"></A>The<SPANCLASS="link"><EMCLASS="emphasis">sl</EM> (<ACLASS="linkend"HREF="ch18_08.htm"TITLE="Showing the Actual Filenames for Symbolic Links ">18.8</A>)</SPAN>script is handy for this - or you can use<SPANCLASS="link"><EMCLASS="emphasis">ls -ld</EM> (<ACLASS="linkend"HREF="ch16_08.htm"TITLE="The ls -d Option ">16.8</A>)</SPAN>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>ls -ld /usr/local/logs</B></CODE>lrwxrwxrwx 1 root 15 Jul 10 1990 /usr/local/logs -> /foo/bar/logs% <CODECLASS="userinput"><B>ls -ld /foo/bar/logs</B></CODE>drwxrwxr-x 2 root 512 Aug 10 12:20 /foo/bar/logs% <CODECLASS="userinput"><B>set mail=(/foo/bar/logs)</B></CODE></PRE></BLOCKQUOTE></P></DIV></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-1220-SECT-1.2">21.8.2 For Bourne Shell Users </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-23388"></A>Now, let's assume that you're a Bourne shell user, and go througheverything once more. The Bourne shell uses three variables tocontrol mail notification.(These are usually set in users'<SPANCLASS="link"><EMCLASS="emphasis">.profile</EM> (<ACLASS="linkend"HREF="ch02_02.htm"TITLE="Shell Setup Files-Which, Where, and Why ">2.2</A>)</SPAN>files.To make them work in<SPANCLASS="link">subshells (<ACLASS="linkend"HREF="ch38_04.htm"TITLE="Subshells ">38.4</A>)</SPAN>,<SPANCLASS="link"><EMCLASS="emphasis">export</EM> (<ACLASS="linkend"HREF="ch06_01.htm#UPT-ART-1170"TITLE="What Environment Variables Are Good For ">6.1</A>)</SPAN>the variables.)We'll assume that you read the C shelldescription already given, and move a bit faster.</P><PCLASS="para">First, if you want to check only one file or directory, set thevariable <EMCLASS="emphasis">MAIL</EM> accordingly.</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>MAIL=/usr/spool/mail/mikel</B></CODE>$ <CODECLASS="userinput"><B>export MAIL</B></CODE></PRE></BLOCKQUOTE></P><BLOCKQUOTECLASS="note"><PCLASS="para"><STRONG>NOTE:</STRONG> The next three features don't work on some Bourne shells.</P></BLOCKQUOTE><PCLASS="para">By default, the Bourne shell checks every ten minutes.<ACLASS="indexterm"NAME="AUTOID-23405"></A>To check at some other interval, set the variable <EMCLASS="emphasis">MAILCHECK</EM> to yournew interval, in seconds: for example, the command below tells theshell to check every 55 seconds:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>MAILCHECK=55</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"> One useful trick: if you set <EMCLASS="emphasis">MAILCHECK</EM> to 0, the shell will checkwhenever it prints the "primary" prompt (by default, <CODECLASS="literal">$</CODE>).In otherwords, it will check after each command.This may be slow on busy systems.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-23415"></A>If you want to watch several files, use the <EMCLASS="emphasis">MAILPATH</EM> variable. Itsvalue must be a list of file or directory names, separated by colons.For example:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>MAILPATH=/usr/spool/mail/mikel:/usr/spool/mail/prodsupport</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">If <EMCLASS="emphasis">MAILPATH</EM> is set, the shell will ignore the <EMCLASS="emphasis">MAIL</EM>variable. You can't use both.</P><PCLASS="para">Normally,the Bourne shell prints <CODECLASS="literal">you have mail</CODE> whenever any file that it'swatching changes. However, if you follow a filenamein <EMCLASS="emphasis">MAILPATH</EM> with a percent sign (<CODECLASS="literal">%</CODE>) and a message,the shell will printthe message whenever the file changes. For example, let's have the shellprint <CODECLASS="literal">you have mail</CODE> when mail comes in and<CODECLASS="literal">New log!</CODE> when a log file changes:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>MAILPATH=/usr/spool/mail/mikel:/home/mikel/Z/log%"New log!"</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">You can create a different message for every file that you care about.Note that the Korn shell and <EMCLASS="emphasis">bash</EM> use <CODECLASS="literal">?</CODE> (a question mark)instead of the <CODECLASS="literal">%</CODE> before each message.</P></DIV><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <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="ch21_07.htm"TITLE="21.7 Setting File Modification Time with touch "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 21.7 Setting File Modification Time with touch "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="ch21_09.htm"TITLE="21.9 Keep File Printouts Up-to-Date Automatically with make"><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 21.9 Keep File Printouts Up-to-Date Automatically with make"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">21.7 Setting File Modification Time with touch </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">21.9 Keep File Printouts Up-to-Date Automatically with make</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 + -