I\'m writing a script in Bash to test some code. However, it seems silly to run the tests if compiling the code fails in the first place, in which case I\'ll just abort the
A SysOps guy once taught me the Three-Fingered Claw technique:
yell() { echo "$0: $*" >&2; }
die() { yell "$*"; exit 111; }
try() { "$@" || die "cannot $*"; }
These functions are *NIX OS and shell flavor-robust. Put them at the beginning of your script (bash or otherwise), try()
your statement and code on.
(based on flying sheep comment).
yell
: print the script name and all arguments to stderr
:
$0
is the path to the script ;$*
are all arguments. >&2
means >
redirect stdout to & pipe 2
. pipe 1
would be stdout
itself. die
does the same as yell
, but exits with a non-0 exit status, which means “fail”. try
uses the ||
(boolean OR
), which only evaluates the right side if the left one failed.
$@
is all arguments again, but different.