Skip to content

Instantly share code, notes, and snippets.

@kam1kaze
Created April 28, 2015 23:43
Show Gist options
  • Save kam1kaze/05f655b8e4e4fdcf027e to your computer and use it in GitHub Desktop.
Save kam1kaze/05f655b8e4e4fdcf027e to your computer and use it in GitHub Desktop.
Template for shell script with logging
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