Created
December 11, 2014 23:48
-
-
Save kapadia/c193ca148f510eab1c52 to your computer and use it in GitHub Desktop.
Bash Error Handling
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 | |
# Use -e to exit the script as soon as anything returns non-zero | |
set -eou pipefail | |
function usage() { | |
echo "" | |
echo "Template script for bash error handling." | |
echo "" | |
echo "Usage: ./scaledown" | |
echo "" | |
} | |
# Define an error handling function | |
function onerror() { | |
# Do what you want when catching an error | |
exit 0 | |
} | |
# Tell the script to trigger `onerror` immediantly before exit | |
trap onerror EXIT | |
# Whatever you want your script to do | |
function go() { | |
echo "hi" | |
} | |
# Call your function | |
go | |
# Before a successfull exit, unhook the `onerror` function; otherwise | |
# it will trigger on exit | |
trap - EXIT | |
# Set your exit status | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment