Re: proper way to assign return value to a variable

Richard Ball <[email protected]>
Newsgroups gmane.mail.procmail
Message-ID <[email protected]>
On 30-12-2010 9:26, Richard Reina wrote:
> If anyone could please help me with this I would REALLY, REALLY 
> appreciate it.  I find procmail very cryptic for me and I am 
> endangering my job by falling way behind on my other work -- not to 
> mention neglecting my family.  Any help would be greatly appreciated. 
> Currently I have a recipe that calls a perl script like this:
>
> :0 w # send to test_subject.pl <http://test_subject.pl> check for 
> order no.
> * ? /usr/bin/perl -e 'require "/home/richard/.pmdir/test_subject.pl 
> <http://test_subject.pl>"; test_subject($ENV{SUBJECT};'
>
> This works fine, but what I need is to capture the return value of the 
> script so that I can compare it in a conditional and depending on the 
> result use it in another called script.  Something like the following:
>
> :0
> PO=`/usr/bin/perl -e 'require "/home/richard/.pmdir/test_subject.pl 
> <http://test_subject.pl>"; test_subject($ENV{SUBJECT};'`
>
> :0:
> * PO ?? 0  # if PO equals zero. PO is NOT valid
> |( formail -rI"From: Responses will be automatically discarded 
> <[email protected] <mailto:[email protected]>>"; \
>    echo "Your mail has been administratively rejected."; \
>    echo "PO is not valid." \
>  ) | /usr/sbin/ssmtp -oi -t
> :0
> {# else PO is valid strip the attachments and file it
> | ( ripmime -i -d /home/richard/.pmdir/attachments/  )
> | /usr/bin/perl -e 'require "/home/richard/.pmdir/test_subject.pl 
> <http://test_subject.pl>"; rename_attachments($ENV{PO});'
>
> If anyone could please help me.  I would really, really appreciate it. 
> I'm in dire straights.
>
To do anything complex with procmail will require testing in an 
environment where you aren't going to damage real email, so setup 
something that will do that within the context of your system. Such a 
'sandbox' is available by taking a look at the information provided by 
Sean at http://www.professional.org/procmail/disclaimer.html and I 
*really* recommend you take the time to do read and understand it 
(especially if this is important for your job performance) the time 
taken will be worth it in the end.

Are the perl scripts you have written performing properly? If you feed 
them an example email will they do all that is required or are you just 
putting in placeholders that you are hoping will function when you 
finish with them?

Procmail has an E flag which can be used for 'else if' type conditions 
and as was mentioned earlier you need to be aware of the difference 
between delivering and non-delivering recipes. The f flag for 'filter' 
is useful to create a non-delivering recipe if you need to feed the 
message to a program outside procmail to get some work done.

Don't append a : to the :0 construct unless you are writing to a file 
(it means create a lockfile and that can present problems unless you're 
familiar with what is going to happen).

I don't understand why you need to call the perl script twice. Can't it 
test for a valid PO# and if it is one go ahead and strip attachments and 
save the needed information all at once? Then return success or failure 
to tell you whether a valid number was there or not.

Basically:

:0
* ^Subject: (some test here for a PO# expression)   #is this even a 
possible PO# email?
{
    :0fW
    | perl_script_that_does_the_work

    :0a                                      #the a flag will execute 
this recipe if the above recipe succeeded
    use_sendmail_to_tell_me_a_valid_PO_was_received
}

# we're here if the email had nothing to do with PO#s, process further 
or discard as you wish
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.