In the following example I would like foo
to be deleted in case of error. Unfortunately it doesn't work.
foo:
perl -e 'die()' > $@ || [rm $@ -a true]
What is it wrong?
In the following example I would like foo
to be deleted in case of error. Unfortunately it doesn't work.
foo:
perl -e 'die()' > $@ || [rm $@ -a true]
What is it wrong?
GNU make can do that for you.
Special Built-in Target Names:
.DELETE_ON_ERROR
If
.DELETE_ON_ERROR
is mentioned as a target anywhere in the makefile, then make will delete the target of a rule if it has changed and its recipe exits with a nonzero exit status, just as it does when it receives a signal.
It is a general problem that creating a file is a non-atomic operation. And not always you can delete an incomplete or corrupted file on termination, for example, when the program is killed with SIGKILL
or by the OOM-killer. In other words, all solutions involving removing the file are prone to failures.
The robust generic solution is:
E.g.:
foo:
perl -e 'die()' > $@~
mv --force $@~ $@
This works for me…
foo:
@perl -e 'die()' > $@ || { echo "removing $@ because exit code was $${?}"; rm $@; }
Died at -e line 1.
removing foo because exit code was 255