So I have a little script for running some tests.
javac *.java && java -ea Test
rm -f *.class
Now the problem with this is that when I run the script ./test
, it will return a success exit code even if the test fails because rm -f *.class
succeeds.
The only way I could think of getting it to do what I want feels ugly to me:
javac *.java && java -ea Test
test_exit_code=$?
rm -f *.class
if [ "$test_exit_code" != 0 ] ; then false; fi
But this seems like something of a common problem — perform a task, clean up, then return the exit code of the original task.
What is the most idiomatic way of doing this (in bash or just shells in general)?
Best Answer
You can wrap the
exit
andrm
commands up into a single simple-command witheval
like:That way
$?
's value when passed toexit
is whatever it gets assigned immediately beforeeval
runs.