You are not logged in.
The idiom
work || exit 1
(where exit might be return)
works well, but I'd like giving the user something more visual than the mere non-zero return.
At the moment I use:
work
if [[ $? -ne 0 ]] ;then
echo "An error occurred while doing work!"
exit 1
fi
But it seems so verbose... There is a easy way just to say `on error leave a message and exit(return)' ?
Thanks.
Offline
You could define a helper function.
function lexit()
{
message="$1"
exitCode=$2
echo "ERROR: $message"
exit $exitCode
}
work || lexit "Couldn't perform work!" 1
Offline
work || echo "kablooie" && exit 1
Offline
@Cerebral
It does not work (does it?) for return, but it works great for exit. Thanks.
@tam1138
$ false || echo A && echo B
A
B
$ true || echo A && echo Opsss
Opsss
Offline
Adding parentheses helps:
$ false || ( echo A && echo B )
A
B
$ true || ( echo A && echo B )
$
Offline
*chuckles*
My own 2c worth ... guess we all have our different ways
All of my shell-scripts starts with this:
say() { echo -en "$@" >&2; }
die() { say "$@\n"; exit 1; }
then - somewhere further down
do_something || die "this didn't work!"
Offline