here is my shell script, I tried to catch the failing command and print the its result:
myfunc() {
$1
uptime
echo "-------------------------------------"
EXIT_STATUS=$?
if [ ! "$EXIT_STATUS" = "0" ]; then
echo "Exit on failure"
else
echo "Exit on success"
fi
}
myfunc pwd
myfunc failfail
which results in:
/root
15:19:54 up 15:43, 3 users, load average: 0.01, 0.09, 0.09
-------------------------------------
Exit on success
test2.sh: line 4: failfail: command not found
15:19:54 up 15:43, 3 users, load average: 0.01, 0.09, 0.09
-------------------------------------
Exit on success
which second time should print Exit on failure
, I tried with set -e
but actually it exit the code and not continue to if else
statement.