Skip to content

Instantly share code, notes, and snippets.

@ahendrix
Created October 17, 2013 18:56
Show Gist options
  • Save ahendrix/7030300 to your computer and use it in GitHub Desktop.
Save ahendrix/7030300 to your computer and use it in GitHub Desktop.

Revisions

  1. ahendrix created this gist Oct 17, 2013.
    24 changes: 24 additions & 0 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    function errexit() {
    local err=$?
    set +o xtrace
    local code="${1:-1}"
    echo "Error in ${BASH_SOURCE[1]}:${BASH_LINENO[0]}. '${BASH_COMMAND}' exited with status $err"
    # 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 ERR to provide an error handler whenever a command exits nonzero
    # this is a more verbose version of set -o errexit
    trap 'errexit' ERR
    # setting errtrace allows our ERR trap handler to be propagated to functions,
    # expansions and subshells
    set -o errtrace