Run makefile command from bash script and return e

2020-04-27 01:38发布

问题:

I have a python makefile. I can run its commands from my bash script as below

local make_lint_output=""
make_lint_output="$( make test-unit  2>&1 )"
echo "${make_lint_output}"

local result=$? 
if (( result == 0 )); then
    return 1
fi 

But the problem is it always return $? as 0 even though make command exits with an error.

On failure part of the output is like below

E ImportError: No module named 'serial' !!!!!!!!!!!!!!!!!!! Interrupted: 3 errors during collection !!!!!!!!!!!!!!!!!!!! =========================== 3 error in 0.17 seconds ============================ Makefile:61: recipe for target 'test-power-control' failed

$? should be return other than 0 in this case. What am I missing here? I m running the bash script in unix machine.

回答1:

echo is succeeding, and thus returning 0. You have to capture the return code before running another command that may clobber $? (before the echo).