exit with error message in bash (oneline)
exit
doesn't take more than one argument. To print any message like you want, you can use echo
and then exit.
[[ $TRESHOLD =~ ^[0-9]+$ ]] || \
{ echo "Threshold must be an integer value!"; exit $ERRCODE; }
You can use a helper function:
function fail {
printf '%s\n' "$1" >&2 ## Send message to stderr.
exit "${2-1}" ## Return a code specified by $2, or 1 by default.
}
[[ $TRESHOLD =~ ^[0-9]+$ ]] || fail "Threshold must be an integer value!"
Function name can be different.
Using exit
directly may be tricky as the script may be sourced from other places. I prefer instead using subshell with set -e
(plus errors should go into cerr, not cout) :
set -e
[[ $TRESHOLD =~ ^[0-9]+$ ]] || \
(>&2 echo "Threshold must be an integer value!"; exit $ERRCODE)