procmail
[Top] [All Lists]

Re: $VARIABLE Condition

2004-04-18 07:09:00
Jack asked,

So I'm looking for a condition to check if "$MAILBOX" is defined or
not  ...  I'm thinking something like:
---
* $MAILBOX
---
but if I understand, this will do "egrep $MAILBOX" on the message,
which isn't what I want.

Actually, it would egrep the header for MAILBOX (insensitively to case if there's no D flag); the leading dollar sign would be taken as a modifier to say "evaluate variables and command substitutions in the rest of the condition first," and there are none, so MAILBOX would evaluate to MAILBOX.

OK, you want a condition that will pass if a variable is defined and fail if it is not. Part of the problem is that "defined" is ambiguous; if the variable is set but null, do you consider that "defined"? First time through, let's say you do, and the question becomes how to make procmail determine whether a variable is set, even if it's null:

* $ ! ${variable+!}

in your case,

* $ ! ${MAILBOX+!}

The first $ says to do variable substitution. The braced variable evaluates to ! if MAILBOX is set or to nothing if MAILBOX is unset.
With the additiona exclamation point, the entire thing reduces to

* !!

(which is always true) if the variable is set or to

* !

(which is always false) if the variable is unset.

Now, if by defined you meant "set and not null," use

* $ ! ${MAILBOX:+!}

instead (note the colon before the plus sign). It will act just like the other except that it will treat a null variable as if it weren't set.

Ruud suggested

* MAILBOX ?? .

which takes a different logic: it passes if the variable contains at least one character that isn't a newline and fails if the variable is unset, is null, or contains nothing but newlines. If the MAILBOX variable is actually for the name of a mailbox, it's extremely unlikely that you'd give a mailbox a null name or a name made up of all newlines, so it would work out the same. If it *could* have a name that's nothing but newlines,

* ! MAILBOX ?? ^^^^

would allow for that, while an unset or null variable will still fail the condition.


_______________________________________________
procmail mailing list
procmail(_at_)lists(_dot_)RWTH-Aachen(_dot_)DE
http://MailMan.RWTH-Aachen.DE/mailman/listinfo/procmail

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