Last active
February 15, 2019 08:49
-
-
Save prisis/43a2a7b137998ac92e24ee4daaa8e296 to your computer and use it in GitHub Desktop.
Ported try catch functions to bash.
This file contains 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
#!/usr/bin/env bash | |
# How to use: | |
# | |
# And add it to your bash file | |
# source ./try_catch.sh | |
# | |
# try | |
# ( | |
# your code | |
# ) | |
# catch || { | |
# exit 1 | |
# } | |
function try () { | |
[[ $- = *e* ]]; SAVED_EXCEPTION=$? | |
set +e | |
} | |
function catch () { | |
export ex_code=$? | |
(( $SAVED_EXCEPTION )) && set +e | |
return $ex_code | |
} | |
function throw() { | |
exit $1 | |
} | |
function throwErrors() { | |
set -e | |
} | |
function ignoreErrors() { | |
set +e | |
} | |
export -f try; | |
export -f catch; | |
export -f throw; | |
export -f throwErrors; | |
export -f ignoreErrors; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Download it with:
wget https://gistcdn.githack.com/prisis/43a2a7b137998ac92e24ee4daaa8e296/raw/681b89b8e156750de46558ead661509c468fb9a2/try_catch.sh