Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a TRY CATCH command in Bash

I'm writing a shell script and need to check that a terminal app has been installed. I want to use a TRY/CATCH command to do this unless there is a neater way.

like image 653
Lee Probert Avatar asked Feb 25 '14 09:02

Lee Probert


People also ask

Does bash have a try catch?

The bash shell does not have any fancy exception swallowing mechanism like try/catch constructs. Some bash errors may be silently ignored but may have consequences down the line. The bash shell does not even have a proper debugger.

What is the test command in bash?

In bash shell, the test command compares one element against another and returns true or false. In bash scripting, the test command is an integral part of the conditional statements that control logic and program flow.

Does a bash script stop on error?

Bash Pitfalls Failed commands do not stop script execution In most scripting languages, if a function call fails, it may throw an exception and stop execution of the program. Bash commands do not have exceptions, but they do have exit codes.


2 Answers

Based on some answers I found here, I made myself a small helper file to source for my projects:

trycatch.sh

#!/bin/bash  function try() {     [[ $- = *e* ]]; SAVED_OPT_E=$?     set +e }  function throw() {     exit $1 }  function catch() {     export ex_code=$?     (( $SAVED_OPT_E )) && set +e     return $ex_code }  function throwErrors() {     set -e }  function ignoreErrors() {     set +e } 

here is an example how it looks in use:

#!/bin/bash export AnException=100 export AnotherException=101  # start with a try try (   # open a subshell !!!     echo "do something"     [ someErrorCondition ] && throw $AnException      echo "do something more"     executeCommandThatMightFail || throw $AnotherException      throwErrors # automaticatly end the try block, if command-result is non-null     echo "now on to something completely different"     executeCommandThatMightFail      echo "it's a wonder we came so far"     executeCommandThatFailsForSure || true # ignore a single failing command      ignoreErrors # ignore failures of commands until further notice     executeCommand1ThatFailsForSure     local result = $(executeCommand2ThatFailsForSure)     [ result != "expected error" ] && throw $AnException # ok, if it's not an expected error, we want to bail out!     executeCommand3ThatFailsForSure      # make sure to clear $ex_code, otherwise catch * will run     # echo "finished" does the trick for this example     echo "finished" ) # directly after closing the subshell you need to connect a group to the catch using || catch || {     # now you can handle     case $ex_code in         $AnException)             echo "AnException was thrown"         ;;         $AnotherException)             echo "AnotherException was thrown"         ;;         *)             echo "An unexpected exception was thrown"             throw $ex_code # you can rethrow the "exception" causing the script to exit if not caught         ;;     esac } 
like image 20
Mathias Henze Avatar answered Sep 21 '22 18:09

Mathias Henze


Is there a TRY CATCH command in Bash?

No.

Bash doesn't have as many luxuries as one can find in many programming languages.

There is no try/catch in bash; however, one can achieve similar behavior using && or ||.

Using ||:

if command1 fails then command2 runs as follows

command1 || command2 

Similarly, using &&, command2 will run if command1 is successful

The closest approximation of try/catch is as follows

{ # try      command1 &&     #save your output  } || { # catch     # save log for exception  } 

Also bash contains some error handling mechanisms, as well

set -e 

it stops your script if any simple command fails.

And also why not if...else. It is your best friend.

like image 136
Jayesh Bhoi Avatar answered Sep 20 '22 18:09

Jayesh Bhoi