procmail
[Top] [All Lists]

Re: problem with environment variable which is lost

1997-10-07 09:12:44
On Tue, 7 Oct 1997 15:22:40 +0200 (MET DST),
"Denis B. Roegel" <Denis(_dot_)Roegel(_at_)loria(_dot_)fr> wrote:
:0:
|rm -f headers.tmp;echo "From:" $FROM >> headers.tmp; \
echo "To:" $TO >> headers.tmp;echo "Cc:" $CC >> headers.tmp; \
echo "Found:" $FOUND >> headers.tmp;RESULT=`myfilter headers.tmp`; \
echo $RESULT >> result.list1

This is rather inelegant, but more about that in a moment.

# If RESULT is non empty, put the message in `my-default' folder

This cannot work, because the shell snippet above is run in a
subshell. Parents never inherit from their children, remember? 

Anyhow, here's a hopefully somewhat more elegant (though untested),
ahem, "improvement":

    # If myfilter cannot read from standard input, rewrite it so it can

    :0h:"result.list1$LOCKEXT"
    RESULT=| ( formail -XFrom: -XTo: -XCc: ; echo "Found: $FOUND" ) | \
        myfilter | tee -a result.list1

    # If you actually want the exact values of $TO and $FROM, etc, 
    #  by all means use what you had before. I'm guessing formail -X
    #  is good enough for you.

:0:
#* RESULT ?? [^      ]
|echo $RESULT >> result.list2
my-default

(Is this a typo? Or do you actually try to have two actions on one
recipe?)

This could of course be accomplished by another addition to the above
shell script just as well, and is probably no less efficient. 

    ... | grep '[^      ]' >> result.list2

(... but then you end up not locking one or both of the result files.
Perhaps this is not a problem.)

Finally, this in fact looks like you could accomplish what you want
with a few judiciously chosen LOGFILE= and LOG= statements.
(Incidentally, the log is also never locked, so if you want some kind
of locking, you'll need a regional lockfile somewhere, too.)
The sort of data you want to write definitely looks like some sort of
logging, is this correct?

    # Save old value of LOGFILE if it was set to something
    :0
    * LOGFILE ?? .
    { OLDLOG="$LOGFILE" }

    # then set it to the file we want to write stuff to
    LOGFILE=result.list1

    # Perhaps you could rewrite myfilter even more so that it actually
    #  greps out the stuff it wants from the mail headers itself?

    :0h
    * # whatever conditions you had before ...
    RESULT=| myfilter FOUND="$FOUND"
                    # ^^^^^^^^^^^^^^
                    # This is actually not necessary because your
                    #  script already gets FOUND exported to its
                    #  environment. If myfilter is a Perl script, 
                    #  try $ENV{"FOUND"}

    :0A
    {
        LOG="$RESULT
"
        :0
        * RESULT ?? .
        {
            LOGFILE=result.list2
            LOG="$RESULT
"
        }
    }

    :0
    * OLDLOG ?? .
    { LOGFILE="$OLDLOG" }

(Well, perhaps this is not so elegant, either :-)

Assuming the first logging is something you do simply because you need
it for debugging your script, consider making the script output it to
STDERR and it will appear in Procmail's log (whatever LOGFILE may be
at the time the script it invoked). If you do that, you can simplify
this even further. In the end, perhaps you only want the script to
output the result.list2 stuff unless in debugging mode.

I you end up wishing to write to the log from a shell script, the >&2
redirection operator redirects standard output to standard error.
(This, of course, doesn't work with tcsh, but that should come as no
surprise. Always do SHELL=/bin/sh anyhow ;^!

If my assumptions are correct, this could all, then, be trimmed down
to the remarkably elegant

    :0h
    | myfilter

but of course, I might have assumed the wrong things. 

(A more likely usage is one whereby you want myfilter to judge whether
you want to keep a message, in which case you might have

    :0
    * ! ? myfilter
    /dev/null

and then, of course, myfilter has to return a suitable exit code in
all cases.)

What am I doing wrong ? Besides, are there other stupid/dangerous
things in what I wrote above ?

As has been pointed out before, anything that uses a temp file looks
like it could be done better. You might want to look at some shell
programming book, such as the Kernighan/Pike classic or one of the
O'Reilly tomes. Reading comp.unix.shell is a great waste of time but
can also be a great way to learn. Just make sure you have a big enough
kill file :-)

Hope this helps,

/* era */

If the locking stuff confuses you, a hopefully less confusing
treatment can be found in the FAQ at
<http://www.iki.fi/~era/procmail/mini-faq.html> -- if you think that,
too, is confusing, I'd like to try to explain it better, so any
feedback is most welcome.

-- 
 Paparazzi of the Net: No matter what you do to protect your privacy,
  they'll hunt you down and spam you. <http://www.iki.fi/~era/spam/>

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