procmail
[Top] [All Lists]

Re: [autoresponder] testing existence of requested file

1999-11-29 00:20:43
On Mon, 29 Nov 1999 07:33:23 +0100, Rejo Zenger
<subs(_at_)sisterray(_dot_)xs4all(_dot_)nl> wrote:
With this part i'll try to test if the file exists:
        FILE_TEST = `/usr/bin/test -f $FILEDIR2/$MATCH`
        :0Ehcw
        * $TEST_TEST ?? 0
As far as i know the output of 'test -f <file>' should be 0 if true
and 1 if false. So, testing for 0 or 1 should tell me whether the
file exists, isn't it?

No, the output of `test` is the empty string.

test(1) also coincidentally sets its error code, but it doesn't print
it. It doesn't print anything, so the output of the `backticks` will
always be the empty string, regardless of whether the file exists.

You could do `test && echo Yessir` but that's just silly. Try this
instead: 

    :0 wc #flags ... what's the E for? What's the h for?
    * test -f $FILEDIR/$MATCH
    ... whatever

You were also comparing TEST_TEST while setting FILE_TEST but that
wouldn't have been so hard to catch ... (Philip: How about a facility
for warning about testing unset variables, like perl -w or something?)

Any idea's what my problem is here? I guess it is because the
exitcode of test is zero or one, not it's output. True? And if so,
how to capture that exitcode instead of it's output?

Ah yes, you're on the right track. The normal and idiomatic way to run
a program and test its exit code is the ? modifier to a condition, but
if you really want to avoid that for some reason, you could also do
something like

    DUMMY=`backticks`
    RESULT=$?

    ... do something with $RESULT

I can't think of a reason to do this, other than if you actually want
to capture the output +and+ the exit code (in which case DUMMY is not
an appropriate name for the first variable :-)

Hope this helps,

/* era */

-- 
 Too much to say to fit into this .signature anyway: <http://www.iki.fi/era/>
  Fight spam in Europe: <http://www.euro.cauce.org/> * Sign the EU petition