procmail
[Top] [All Lists]

Re: Avoiding unnecessary call

2001-01-06 15:35:06
Dallman Ross <dman(_at_)nomotek(_dot_)com> writes:
From: Rejo Zenger <subs(_at_)sisterray(_dot_)xs4all(_dot_)nl>
...
<recipes elided>
...
I have this call (#2) to grep to find out if a $NG and 'running' can be
found in a specific file. if the recipe just before that call is not
matched (#1) the recipe at #3 will not be executed. However the call
will still be done - i understand this.

Is there a easy way to avoid that call if the recipe at #1 is not
matched? I can think of another nested recipe, but isn't there a more
easy way?

If I undertand you, then I think this will work:

  :0
  * ^Envelope-To: stemformulieren(_at_)sisterray\(_dot_)xs4all\(_dot_)nl
  {
          :0                                                          
          * ^Subject: .*\/nl\.[^   ]+
          {
             NG = $MATCH

             :0
             |RUNNING=`grep -i ${NG}.*Running ${VOTEDIR}/stemmingen.txt`
          }
  
          # Is CFV for $NG running? If yes, save in it's directory.   
          :0A
          * RUNNING ?? $NG
          {
                  [whatever]
                  $VOTEDIR/$NG/votes
          }
   }

Well, you saved the call to grep when NG doesn't get set, but you
changed the call to not work.  The original recipe set the RUNNING
variable like this:

          RUNNING=`grep -i ${NG}.*Running ${VOTEDIR}/stemmingen.txt`


You moved into a block and changed it to:

             :0
             |RUNNING=`grep -i ${NG}.*Running ${VOTEDIR}/stemmingen.txt`

That doesn't work: the action will be invoked in a subshell, so the
variable assignment won't be seen by procmail.  There are two solutions:
go back to simple variable assignment like he had, or rearrange the action:

        :0 i
        RUNNING=|grep -i "${NG}.*Running" ${VOTEDIR}/stemmingen.txt

Note that since the grep is not reading its stdin, you need the 'i'
flag on the recipe to tell procmail to not complain about the action
not reading the entire message.


Make that three solutions: rather than capturing grep's output, just
check its return code:

        :0
        * ^Envelope-To: stemformulieren(_at_)sisterray\(_dot_)xs4all\(_dot_)nl
        * ^Subject: .*\/nl\.[-a-z_0-9.]+
        * ? grep -i "${MATCH}.*Running" ${VOTEDIR}/stemmingen.txt >/dev/null
        {
            :0 c:
            $VOTEDIR/$MATCH/backup/votes

            :0:
            $VOTEDIR/$MATCH/votes
        }

That's efficient because procmail will skip the rest of the conditions
on a recipe as soon as non-scoring condition fails, or if a scoring
condition pushes the accumulated score to "negative infinity**".  So,
if the subject doesn't match, the grep won't run.

Note that I tightened up the regexp used to match the newsgroup name,
to keep people from putting "*"s and the like in to scan for votes
going on.  Hmm, are accented characters allowed and/or used in newsgroup
names in Europe?

I also quoted the match in the grep command.  That'll keep the shell
from trying to perform filename expansion on the pattern argument to
the grep command.  Hmm, You could actually get a false match there if
two newsgroups differ by a dot, or if one is a substring of another.
We would need to know more about the format of the stemmingen.txt file
to fix that.  Knowing your version of grep (or OS) may make that
easier.


Philip Guenther
Procmail Maintainer

** Infinity == 2147483647.0
_______________________________________________
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>