I am making a presubmit script. It looks like this:
function presubmit() {
gradle test android
gradle test ios
gradle test server
git push origin master
}
I want the function to exit, if any of the tests fail so it won't push a bug to git. How?
There are many methods to quit the bash script, i.e., quit while writing a bash script, while execution, or at run time. One of the many known methods to exit a bash script while writing is the simple shortcut key, i.e., “Ctrl+X”. While at run time, you can exit the code using “Ctrl+Z”.
To end a shell script and set its exit status, use the exit command. Give exit the exit status that your script should have. If it has no explicit status, it will exit with the status of the last command run.
By default, a function returns the exit code from the last executed command inside the function. It will stop the function execution once it is called. You can use the return builtin command to return an arbitrary number instead. Syntax: return [n] where n is a number.
Within a bash script, you may use the set command to turn exit on error on, set -e. For more details on errexit, see how to debug bash script. Trap allows us to set commands to run in the event the shell receives a signal.
Knowing how to use exit codes, options such as errexit, and traps allow you to create robust scripts and better handle errors in bash. Handling errors based on exit codes is the standstill method for detecting failure in a command. This is especially true in the case of external commands.
Exit Status: Returns N, or failure if the shell is not executing a function or script. Note that if you have set -e set at the top of your script and your return 1 or any other number besides 0, your entire script will exit. @YevgeniyBrikman that's only true if the error in the function is unexpected.
I have outlined the foundation needed to handle any error in bash. Knowing how to use exit codes, options such as errexit, and traps allow you to create robust scripts and better handle errors in bash. Handling errors based on exit codes is the standstill method for detecting failure in a command.
1. Use subshell ( .. )
with set -e
; to make it more concise, you can do this:
build() {( set -e # Fail early
build_cmd_step_1
build_cmd_step_2
build_cmd_step_3
...
)}
Then, the function will fail on the first failure and you can intercept the exit status:
build
exit_status=$?
if [ ${exit_status} -ne 0 ]; then
echo "We have error - build failed!"
exit "${exit_status}"
fi
2. Alternatively, the && \
chaining inside a function is also good (https://stackoverflow.com/a/51913013/1375784), though it might get bad if you have a bigger function.
Both methods can be good, depending on your use case (in some cases using subshells may cause some unwanted side effects)
The way I do it is to add && \
after every command in the function (except the last one).
function presubmit() {
gradle test android && \
gradle test ios && \
gradle test server && \
git push origin master
}
I would make script more granulated:
#!/bin/bash
function test() {
gradle test android
gradle test ios
gradle test server
}
function push() {
git push origin master
}
# this subshell runs similar to try/catch
(
# this flag will make to exit from current subshell on any error inside test or push
set -e
test
push
)
# you catch errors with this if
if [ $? -ne 0 ]; then
echo "We have error"
exit $?
fi
We track error only inside test and push. You can add more actions outside of subshell where test and push run. You can also this way add different scope for errors (let consider it as try/catch)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With