procmail
[Top] [All Lists]

Re: Variable checking routine...

1997-12-02 04:06:09
Catherine wrote,

I want to write a recipe which checks to see if a variable is set, and if
It isn't, sets it to a default value.  Will this recipe work?

:0:
* ! VARIABLE ?? .*
VARIABLE=defaultvalue

In short, no. In long:

This is not a good recipe. First, you specify locking. You intend a
non-delivering recipe, and should not specify a lockfile at all.
Second, you are testing to see if .* is in $VARIABLE. .* matches
anything _or nothing_. Finally, you go ahead and deliver to a mailbox
named VARABLE=defaultvalue (which does need a lockfile after all)
instead of changing the value of the variable! The way your condition
is written, your recipe will never test true, and will never deliver. I
suspect that you meant

 :0
 * VARIABLE ?? ^^^^
 { VARIABLE=defaultvalue }

but that can be replaced by either of

 VARIABLE=${VARIABLE:-defaultvalue}

and

 VARIABLE=${VARIABLE-defaultvalue}


These are standard shell variable substitution mechanisms which are
supported by procmail. Other variants which may prove useful are as
follows. Note that VAR1 may (or may not, as you wish) be the same as
VAR2.

 VAR1=${VAR2:+replacement}
 VAR1=${VAR2+replacement}

The differences are as follows.
 :- sets VAR1 to VAR2 if VAR2 is set and non-null, and sets VAR1 to
    default otherwise.
 -  sets VAR1 to VAR2 if VAR2 is set, and sets VAR1 to default
    otherwise.
 :+ sets VAR1 to replacement if VAR2 is set and non-null, and sets VAR1
    to VAR2 otherwise.
 +  sets VAR1 to replacement if VAR2 is set and sets VAR1 to VAR2
    otherwise.

The way you phrased your request, you want the second version, although
the first version, which will replace a null value with the new value,
may be appropriate as well, depending on the specific meaning you
intend.

-- 
Rik Kabel          Old enough to be an adult              
rik(_at_)netcom(_dot_)com

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