Reputation: 33445
I have a makefile which succeeds where it should fail because a line like this
./preprocess.sh <PARTIAL_SOURCE | perl >FINAL_SOURCE
succeeds even though PARTIAL_SOURCE
doesn't exist yet.
This isn't a quirk of preprocess.sh
, it seems to be something to do with bash/sh
$> cat <does_not_exist && echo ok || echo no
bash: does_not_exist: No such file or directory
no
$> cat <does_not_exist | perl && echo ok || echo no
bash: does_not_exist: No such file or directory
ok
Why does the first fail but the second succeed?
Upvotes: 1
Views: 43
Reputation: 124724
The second succeeds because perl
succeeds. The exit code of a pipeline is the exit code of the last command in the pipeline. In this example it's perl
. It receives empty input, does nothing with it, so it exits with 0, as it should.
As another example, this is successful too:
$ a | b | perl && echo ok || echo no
-bash: a: command not found
-bash: b: command not found
ok
If you don't want perl
to get executed before PARTIAL_SOURCE
is ready,
you need to test for it before the pipeline:
if [ ! -f "$input" ]; then
./preprocess.sh < "$input" | perl >FINAL_SOURCE
fi
Or wait for the input to be ready:
while [ ! -f "$input" ]; do sleep 1; done
Upvotes: 2