Created
April 28, 2015 23:43
-
-
Save kam1kaze/05f655b8e4e4fdcf027e to your computer and use it in GitHub Desktop.
Template for shell script with logging
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
set -o pipefail | |
set -o errtrace | |
set -o nounset | |
set -o errexit | |
exec 1> >(while IFS= read -r line; do echo "$(date) [DEBUG] $line"; done >>/tmp/test) | |
exec 2> >(while IFS= read -r line; do echo "$(date) [ERROR] $line"; done >>/tmp/test) | |
date | |
function finish { | |
echo TRAP | |
exec &>/dev/tty | |
echo "[ERROR]: code: $1, line: $2, func: $3" | |
} | |
function fail_func() { | |
ls -la weqwewqewq | |
} | |
trap 'finish $? $LINENO ${FUNCNAME:-main}' ERR | |
echo start script | |
sleep 2 | |
date | |
#ls -la cxzczxczc | |
fail_func | |
echo end script |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment