procmail
[Top] [All Lists]

Re: if else

1998-05-30 07:08:14
John M Vinopal <banshee(_at_)abattoir(_dot_)com> writes:

Could someone confirm the basic format for the following pseudocode:

if (X) { set Y = X }
elseif (X2) { set Y = X2 }
elseif (X3) { set Y = X3 }

if (Y set) { do things }
else { do other things }

I have part of a template below, but I'm really uncertain about how
to finally check if A) any of the rules worked or (equivalently) B)
$SPAM is set to some value.  Does the 'A' modifier check ANY of the
preceeding rules or just the last one?


# if
:0
* ^Subject: *SPAM:
{ SPAM="spam" }

# else if
:0 E
* ^Subject: Advert
{ SPAM="advert" }

# else if...

# if (Y set)
#XXX how to do this?
:0 A :
* if ($SPAM)

The easiest way to check whether a variable is set (not just whether it
contains a non-empty value) is the following condition:

        * $ ! ${SPAM+!}

The expression "${SPAM+!}", when expanded under bourne shell rules, is
replaced with nothing if the variable SPAM is not set, and is replaced
with '!' if it is set.  Thus, if SPAM is set, then the resulting
condition (after the shell expansion triggered by the leading '$') is
"! !", which evaluated to true (double negation of the null regexp),
while if SPAM is not set, then there's only one '!', and the condition
fails.


{
| $FORMAIL -A"X-Sorted: *** $SPAM ***" >> $SPAMFOLDER
}

You either need to remove the braces, or hang the middle line off on
another recipe:

        {
            :0
            | $FORMAIL -A"X-Sorted: *** $SPAM ***" >> $SPAMFOLDER
        }

When procmail sees an open brace as an action, it expects more recipes
and assignments from there to the close brace, not just orphaned action
lines.

BTW: you probably want to put a lockfile on that recipe, so that
$SPAMFOLDER doesn't get corrupted by simultaneous writes.  Since the
action has a >> in it, you can just put a second colon at the end of
the ":0" recipe start line.


# Else just reinject the mail.
:0
! -oi -f "$@"

If you have procmail 3.11pre4 or later, then you don't need the "-oi", as
procmail includes it for you via the SENDMAILFLAGS variable.


Philip Guenther

<Prev in Thread] Current Thread [Next in Thread>
  • if else, John M Vinopal
    • Re: if else, Philip Guenther <=