Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
oops - on failure, it exited from my shell! Any alternative? –  kfmfe04 Dec 25 '11 at 20:35
    
@kfmfe04 I thought you called this as a shell script program, my bad. You can always simply return without doing the next steps. –  Daniel Beck Dec 25 '11 at 20:41
    
tyvm - works great! –  kfmfe04 Dec 25 '11 at 20:57
    
@kfmfe04 There was a typo in one of the snippets I only just now fixed. Sorry about that. –  Daniel Beck Dec 26 '11 at 9:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.