Reputation: 762
I'm trying to make by bash script do a "not" on an abort so that when a called script returns ok it aborts, and vice versa when it does not abort.
I'm trying this like
#!/bin/bash
set -e
my_process || echo "Program failed as expected. Good!"
my_process ; echo "Program did not fail as expected. Bad!" ; exit -1
but i don't see how to combine the two.
Upvotes: 1
Views: 48
Reputation: 149804
As an alternative to if
/else
, you could use the &&
and ||
operators with groups of commands:
my_process && { echo "Program did not fail as expected. Bad!"; exit -1; } \
|| echo "Program failed as expected. Good!"
Upvotes: 2
Reputation: 9866
Extending from John's answer, using your shorthand:
! my_process && echo "Program failed as expected. Good!" || echo "Program did not fail as expected. Bad!" && exit -1
The !
operator negates the conditional boolean, so you can use && .. || ..
.
EDIT Note that this isn't nearly as clear as John's much more useable code - just to show you can do it with the ternary-style syntax. Prefer the readability of John's answer.
Upvotes: 2
Reputation: 361635
if my_process; then
echo "Program did not fail as expected. Bad!"
exit -1
else
echo "Program failed as expected. Good!"
fi
Upvotes: 3