procmail
[Top] [All Lists]

Re: Variables in output of external programs

1999-05-25 22:42:57
Gregory Sutter <gsutter(_at_)pobox(_dot_)com> writes:
...
It isn't quoted in the .procmailrc file. It is in the external file that
was 'cat'ted on the preceding line.  Following up on another tip, I tried,
instead of just VAR=`cat foo`, VAR=`sh -c "expr cat foo"` but that didn't
work either.

Here is the recipe:

JFBODYCHK=(`sh -c "eval cat $JFDIR/jf-bodychk"`)

Ah, the trick is that you need the 'cat' to take place before the
'eval'.  Consider the following:

$ echo '$BAR BAZ' >foo
$ cat foo
$BAR BAZ
$ BAR=blip
$ eval cat foo
$BAR BAZ
$ eval `cat foo`
blip: not found
$ echo `cat foo`
$BAR BAZ
$ eval echo `cat foo`
blip BAZ
$

So, either of the following will work:

        JFBODYCHK = (`sh -c 'eval echo \`cat $JFDIR/jf-bodychk\`'`)
        JFBODYCHK = (`eval echo \`cat $JFDIR/jf-bodychk\`;`)

(The second assumes that SHELLMETAS contains semicolon.)

Alternatively you can do the eval in the condition itself:


:0HB
* $ ${JFBODYCHK:-$JFNOMATCH}
* < 60000
{
       :0B
       * $ ()\/($JFBODYCHK)
       { JFMATCH="$JFSEC: $MATCH" INCLUDERC=$JFDIR/junkfilter.match }
}

Change that to:

        JFBODYCHK = `cat $JFDIR/jf-bodychk`
        :0 HB
        * $ $ ${JFBODYCHK:-\$JFNOMATCH}
        * < 6000
        {
            :0 B
            * $ $ ()\/($JFBODYCHK)
            { JFMATCH="$JFSEC: $MATCH" INCLUDERC=$JFDIR/junkfilter.match }
        }

There are three changes between your version and the above:
1) JFBODYCHK is set to the uneval'ed contents of jf-bodychk;
2) an extra '$' is placed on each condition containing $JFBODYCHK to
   cause it to be expanded the necessary second time; and
3) (the subtle one) all other variable expansions in those conditions
   have a single backslash placed before them to protect them from the
   extra expansion.

Got it?

Finally, I'll note that the above nested recipe could be done as a
single recipe by using the "B ??" syntax on the third condition:

        JFBODYCHK = `cat $JFDIR/jf-bodychk`
        :0 HB
        * $ $ ${JFBODYCHK:-\$JFNOMATCH}
        * < 6000
        * B ?? $ $ ()\/($JFBODYCHK)
        { JFMATCH="$JFSEC: $MATCH" INCLUDERC=$JFDIR/junkfilter.match }

Since the gain in efficiency is negligible, you should only make that
change if you find it as easy or easier to read.


Philip Guenther

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