procmail
[Top] [All Lists]

Re: running "Side effect" shell commands

1998-02-03 08:51:54
Jari Aalto asked,

|     1) What whuld you use and why?
|     2) Is there any real difference between these.
| 
| #1  Nowadays I use this if I want to add a message to biff log
| 
|     dummy = "`echo message: $FROM $SUBJECT >> $biff `

Problems (besides needing either a closing soft quote or no opening soft
quote): you get no locking on the destination file, and unless you put it
inside braces you have to run it on every message unconditionally.  (Also
procmail tries to feed the whole message to a command that won't read it,
but the remedies for that don't help very much.)

| #2  I used to use this, but i don't like the idea of taking up
|     and "delivering" recipe and circumventing it with `c' flag.
| 
|     :0 wc:
|     | echo message: $FROM $SUBJECT >> $biff

That needs an `i' flag, but it locks the destination file and you can add
conditions to it, so it's probably the best.  If the head or the body is less
than one bufferful, you can limit the unnecessariy written data with `h' or
`b', but I think that in most OSes a partial buffer and a full one are the
same amount of effort.

| #3  And recently I realised that i could restrict the printing condition
|     without using delivery action. We use side effect of "?" here.
| 
|     :0
|     *   in-some-cases-go-here
|     * ? echo "$TODAY $FFROM $FSUBJ" >> $HIN
|     { }

We have conditions possible, but there is no locking on the destination file.
You could add it like this:

  :0
  * in-some-cases-go-here
  {
   LOCKFILE=$HIN$LOCKEXT

   :0
   * ? echo "$TODAY $FFROM $FSUBJ" >> $HIN
   { }

   LOCKFILE
  }

provided that you have no other global or regional lockfile in effect at the
time.  Also, you can limit the write attempt this way, but I doubt that the
gain would be significant:

   * unset ?? ? echo "$TODAY $FFROM $FSUBJ" >> $HIN

where "unset" is the name of an unset variable.

|     folks, any comments?

All told, I'd go with method #2 or a variation thereof:

  # drop `w'; if you can use #3, the exit code apparently does not matter
  :0ci:
  * in-some-cases-go-here
  | echo message: $FROM $SUBJECT >> $biff

or

  :0i:
  * in-some-cases-go-here
  dummy=| echo message: $FROM $SUBJECT >> $biff
  
Here's a variation on #3 which might get procmail's insistence on writing the
search area to the exit code test to work in our favor (untested!):

  :0
  * in-some-cases-go-here
  {
   TEXTTOWRITE="$TODAY $FFROM $FSUBJ"
   LOCKFILE=$HIN$LOCKEXT

   :0
   * TEXTTOWRITE ?? ? cat >> $HIN
   { }

   LOCKFILE
  }

The big disadvantage is that the exit code test runs a shell and cat, whereas
the other version ran only a shell (assuming that $SHELL is set to something
that has echo built in).  I present it principally as a curiosity.

Do note that there are two particulars of this example which have affected
my response: that the command does not read input from the message text, and
that the command needs a lockfile.  If either or both were otherwise, I'd
have to reconsider.