Last active
February 20, 2018 16:36
-
-
Save edmondscommerce/3cb18768f6c5c0d978bbf19724667d04 to your computer and use it in GitHub Desktop.
Paste this to the top of your BASH scripts for a much nicer BASH coding experience
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
#!/usr/bin/env bash | |
readonly DIR=$(dirname $(readlink -f "$0")) | |
cd $DIR; | |
set -e | |
set -u | |
set -o pipefail | |
standardIFS="$IFS" | |
IFS=$'\n\t' | |
echo " | |
=========================================== | |
$(hostname) $0 $@ | |
=========================================== | |
" | |
# Error Handling | |
backTraceExit () { | |
local err=$? | |
set +o xtrace | |
local code="${1:-1}" | |
printf "\n\nError in ${BASH_SOURCE[1]}:${BASH_LINENO[0]}. '${BASH_COMMAND}'\n\n exited with status: \n\n$err\n\n" | |
# Print out the stack trace described by $function_stack | |
if [ ${#FUNCNAME[@]} -gt 2 ] | |
then | |
echo "Call tree:" | |
for ((i=1;i<${#FUNCNAME[@]}-1;i++)) | |
do | |
echo " $i: ${BASH_SOURCE[$i+1]}:${BASH_LINENO[$i]} ${FUNCNAME[$i]}(...)" | |
done | |
fi | |
echo "Exiting with status ${code}" | |
exit "${code}" | |
} | |
trap 'backTraceExit' ERR | |
set -o errtrace | |
# Error Handling Ends |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment