Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exit bash function if error?

Tags:

bash

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?

like image 602
Xi 张熹 Avatar asked Nov 04 '16 18:11

Xi 张熹


People also ask

How do you exit a Bash script error?

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”.

How do you terminate a shell script if statement?

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.

How do you exit a function execution in Linux?

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.

How do I exit a bash script with an error?

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.

Why do we use exit codes 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. This is especially true in the case of external commands.

What is exit status in shell script?

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.

How do I handle errors in Bash?

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.


Video Answer


3 Answers

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)

like image 97
awinecki Avatar answered Sep 30 '22 03:09

awinecki


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
}
like image 31
Drew Chapin Avatar answered Sep 30 '22 03:09

Drew Chapin


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)

like image 31
Kostanos Avatar answered Sep 30 '22 04:09

Kostanos