Is Exception handling possible in Unix shell script, which includes calling another scripts internally

Aditya Poluru

I have a Scenario where in I need to fetch some data by triggering another external bash file from my shell script. If I end up with any error output from external bash, My shell script should handle and should go through the fall back approach. But I am actually facing issue with that external bash file, Wherein bash returns (exit 1) in failure cases, which causes my script also to exit and never executing fall back approach. Can anyone guide how to handle the exit from external bash and run my fall back approach.

MIZWOZ cpu

Not sure if this works in sh, but it works in bash. I made a try / except tool out of this, but it will work here too I believe.

#! /bin/bash

try() {
    exec 2> /dev/null
    #direct stderr out to /dev/null

    #main block
    input_function="$1"

    #fallback code
    catch_function="$3" 

    #open a sub shell
    (

    #tell it to exit upon encountering an error
    set -e

    #main block
    "$@"

    )

    #if exit code of above is > 0, then run fallback code
    if [ "$?" != 0 ]; then
        $catch_function
    else
        #success, it ran with no errors
        test
    fi

    #put stderr back into stdout
    exec 2> /dev/tty
}

An example of using this would be:

try [function 1] except [function 2]

Function 1 would be main block of code, and 2 would be fallback function/block of code. Your first function could be:

run() {
  /path/to/external/script
}

And your second can be whatever you want to fall back on. Hope this helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Bash script with smaller scripts internally

Unix Shell Scripts Functions

Unix shell script find out which directory the script file resides?

How does exception handling work internally in Java?

exception handling in korn shell

Change filenames while moving to another folder using unix shell script

Calling another shell script based on getopts args passed

Calling another program in a shell script and using the returned data

script calling another script?

How to add text to end of file via shell script which includes double quotes

Signal handling in a shell script

Error handling in shell script

Shell script error handling

Shell Script - Unix

Arithmetic in Unix shell Script

Unix Shell Script

Shell script, linux, unix, shell

Call another shell script with argument which contains whitespce

Calling function in Shell script

how to run multiple shell scripts one by one in single go using another shell script

how to run multiple shell scripts one by one in single go using another shell script

Is it possible to make a bash shell script interact with another command line program?

Is it possible to make a bash shell script interact with another command line program?

Which exception handler is handling MissingRequestHeaderException?

Embedding shell scripts in another language?

Error-handling in EFI startup shell scripts

Trap handling and implicit subshells in shell scripts

Using Travis CI for testing on UNIX shell scripts

With google forms and scripts is it possible to create a script which will limit the amount of points that user can give in all scale inputs?