procmail
[Top] [All Lists]

Re: procmail testing return codes

1997-05-29 12:48:00
When Era Eriksson suggested this to 
ant(_at_)notatla(_dot_)demon(_dot_)co(_dot_)uk as a way to
save the exit code of a command:

E>     :0
E>     * 1^1 ! ? hashcash
E>     { SCORE=$= }

E> will actually put the exit code of hashcash in the variable SCORE
E> which you can then peruse as you see fit.

I wrote,

T> I wonder what will happen if the command generates a negative exit code,
T> though.  Just in case, perhaps one should do this:

T>       :0
T>       * 1^1 ! ? hashcash
T>       { }
T>       SCORE = $=

To which Roderick Schertler responded,

S> By definition no such thing exists.  A Unix exit code is an 8 bit
S> unsigned value, it is always between 0 and 255 inclusive.

In any case, the command might legitimately exit with return code 0, so I
still recommend leaving the braces empty and assigning SCORE outside them.

If you're further interested in this, read on.

The thing is that I've seen "exit -1" in scripts from time to time, so I
thought it was legitimate.  In my .procmailrc, I tried a few things with

  * 1^1 exit -1 ;

and with

  * ! 1^1 exit -1 ;

and really couldn't get any definitive results.  It turns out that pdksh
(my $SHELL) considers "exit -1" a syntax error (because -1 is not a legal
*option* for exit!) and returns code 1 on those grounds.  The sh here
recognizes -1 as a number but as an illegal one for an exit code (as Roderick
said) and thus returns code 2 for the usage error.  Neither returns 255.

So I guess that all those scripts I've seen that say things like "exit -1"
give unpredictable results, depending on which shell they run under, or that
there must have been some shell at some time for which "exit -1" meant some-
thing different from "exit -2".  I'll have to hope to find such a shell and
try again.

But in any case, for a different reason, my recommendation stands: because
the exit code might be 0, assign SCORE = $= outside the braces, not between
them.

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