Background
In this posting, the following solution was posted:
function cmakerel { if expr match "$PWD" '*bld*' >/dev/null ; then cmake -D.... else echo "Wrong directory!" fi } cmakerel will invoke a cmake command, if PWD has bld in its name.
However, I normally invoke this command like: cmakerel && make check
Question
How do I modify the code above so that it returns a value on failure such that the second part, make check does not get invoked?
Probably returning some kind of non-zero value should work.
1 Answer
Add
return $? below the cmake call to exit the function with the same exit code as cmake, and add
return 1 below the echo call to exit with 1, which is non-zero and therefore indicates an error.
You can alternatively (if you call both commands in combination) add the make call to this function:
cmake [args] && make -j4 Or, to allow e.g. for more fine-grained error handling:
cmake [args] local _ret=$? if [ $_ret -ne 0 ] ; then echo "cmake returned with exit code $_ret, aborting!" >&2 return $_ret fi make -j4 If you call it as a standalone program, i.e. store the function in a shell script, and invoke e.g. as
/path/to/cmakerel.sh then you can use exit instead of return to abort the whole program/subshell.