Skip to content

Instantly share code, notes, and snippets.

@prisis
Last active February 15, 2019 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prisis/43a2a7b137998ac92e24ee4daaa8e296 to your computer and use it in GitHub Desktop.
Save prisis/43a2a7b137998ac92e24ee4daaa8e296 to your computer and use it in GitHub Desktop.
Ported try catch functions to bash.
#!/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