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.
Best Answer
Add
below the
cmake
call to exit the function with the same exit code ascmake
, and addbelow the
echo
call to exit with1
, 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:Or, to allow e.g. for more fine-grained error handling:
If you call it as a standalone program, i.e. store the function in a shell script, and invoke e.g. as
then you can use
exit
instead ofreturn
to abort the whole program/subshell.