So I have to run a basic shell script that is not much more than:
#!/bin/bash
call01
call02
...
call_N
If one of the calls fails (gets killed by the OS because it runs out of memory), what does it have to say for the exit code of the shell script itself?
I have to capture the fail in the dotnet service that launches the script and in test cases (with a .sh that quits by "exit 1") my code works, i.e. I see the exit code. But in this case it seems like the script doesn't fail its exit, even though the failing program gets terminated, I get an error message with a call stack and all, and the script is terminated there.
1 Answer 1
If you want to exit immediately with a non-zero exit status, then set -e is the way to go.
If you want to execute all the jobs and count the ones that fail, then
failed=0
call01 || ((++failed))
call02 || ((++failed))
...
call_N || ((++failed))
echo "$failed jobs failed"
((failed == 0)) || exit 1
For the last line, exit $failed might work, but it depends on the number of jobs: the exit status is a number between 0 and 255.
-
2For the last line, I would really suggest
((!failed)) || exit, as otherwise it'll exit 1 both on success and failure (the script inheriting the exit code by default)... generally||makes more sense for exceptional cases than&&.grawity– grawity2024年11月29日 21:46:57 +00:00Commented Nov 29, 2024 at 21:46