I would like to capture the return code of a Gradle task. Here is a small bash script draft which executes a tasks:
#!/bin/bash
gradlew_return_code=`./gradlew assembleDebug`
echo ">>> $gradlew_return_code"
if [ "$gradlew_return_code" -eq "0" ]; then
echo "Gradle task succeeded."
else
echo "Gradle task failed."
fi
The script does not store the return value but instead the whole console output of the Gradle task.
Please note that the example script is a simplification of a more complex script where I need to capture the return value.
Exit status is in
$?
. Command substitutions capture output....or, if you need compatibility with
set -e
(which I strongly advise against using):...or, if you need to capture both:
Note that I do advise putting the capture on the same line as the invocation -- this avoids modifications such as added debug commands from modifying the return code before capture.
However, you don't need to capture it at all here:
if
statements in shell operate on the exit status of the command they enclose, so instead of putting a test operation that inspects captured exit status, you can just put the command itself in the COMMAND section of yourif
: