I wrote a C++ watchdog that runs a set of scripts to determine whether there is a problem on that system.
The code is a bit hairy so I won't show it here, but it is equivalent to a system call as follow:
int const r(system("/bin/sh /path/to/script/test-health"));
Only, r
is 0 when the script fails because a command is missing in an if
statement. There is the offensive bit of the script:
set -e
[...]
if unknown_command arg1 arg2
then
[...]
The unknown_command
obviously fails since... it is unknown. At that point the script ends because I have the set -e
at the start.
The exit code, though, is going to be 0 in that situation.
Would there be a way for me to get an exit code of 1 in such a situation?
i.e. the question is detecting the error without having to add a test to know whether unknown_command
exists. I know how to do that:
if ! test -x unknown_command
then
exit 1
fi
My point is that when I write that script, I expect unknown_command
to exist as I install it myself, but if something goes wrong or someone copies the script on another system without installing everything, I'd like to know that I got an error executing the script.