procmail
[Top] [All Lists]

Re: Counting mail messages

1998-05-18 12:45:47
Bryan Albright asked,

| I have it set up to pipe messages through a perl script that reads in the
| old message number, adds one to it, then writes the new number back out to
| the file.  Is this the best (loaded question, right?)  way to do it, or is
| there a better (less cpu intensive) way to do it?

There is always a less CPU intensive way than perl.

Suppose the current count of whatever kind of message you're counting is
stored in a file named "file" ... which could be a basename in $MAILDIR
or a path to a file in a different directory.

To save one cat call (and many catcalls) and one attempt to write the
message, the file should contain not just the number but a variable
assignment, such as this:

  COUNT=number

and here we go:

  :0
  * conditions
  {
   LOCKFILE=file.lock # regional lockfile inside braces -- trust me

   INCLUDERC=file # assigns $COUNT without a cat or a write

   :0
   * $ ${COUNT:-0}^0
   * 1^0
   { NEWCOUNT = $= }

   :0chi
   | echo "COUNT=$NEWCOUNT" > file

   LOCKFILE # release regional lock
  }

Total forks: one shell.

<Prev in Thread] Current Thread [Next in Thread>