Created
July 3, 2020 05:02
-
-
Save AlexFBP/d6d523927b47f19f2f711f7ab63fc50b to your computer and use it in GitHub Desktop.
Tiny try/catch bash functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Refer https://stackoverflow.com/a/25180186/3180052 for usage and credits | |
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can load this in your shell or CI/CD with ("long" form, I know it can be a single line)
and then you can write something like: