add combined script easy usage
authorIan Kelling <iank@fsf.org>
Sun, 28 Jan 2018 17:19:39 +0000 (12:19 -0500)
committerIan Kelling <iank@fsf.org>
Sun, 28 Jan 2018 17:19:46 +0000 (12:19 -0500)
README
err [new file with mode: 0644]
gen-err [new file with mode: 0755]

diff --git a/README b/README
index 78873d6d4f7f40b70f463512aff4fb6ec215f5f5..180c18842f59280a33294e1c9f5a6c39ea2295b3 100644 (file)
--- a/README
+++ b/README
@@ -1,5 +1,7 @@
 Functions to deal with errors in bash
 
+err: usually, just source this. contains the others and runs errcatch
+
 errcatch: set a trap on error to do bash-trace and exit
 errallow: undo errcatch
 errexit: exit and print stack trace
diff --git a/err b/err
new file mode 100644 (file)
index 0000000..57ea542
--- /dev/null
+++ b/err
@@ -0,0 +1,69 @@
+#!/bin/bash
+# this file was generated from gen-err and meant to be sourced
+bash-trace() {
+    local -i argc_index=0 frame i start=${1:-1} max_indent=8 indent
+    local source
+    local extdebug=false
+    if [[ $(shopt -p extdebug) == *-s* ]]; then
+        extdebug=true
+    fi
+    for ((frame=0; frame < ${#FUNCNAME[@]}-1; frame++)); do
+        argc=${BASH_ARGC[frame]}
+        argc_index+=$argc
+        ((frame < start)) && continue
+        if (( ${#BASH_SOURCE[@]} > 1 )); then
+            source="${BASH_SOURCE[frame+1]}:${BASH_LINENO[frame]}:"
+        fi
+        indent=$((frame-start+1))
+        indent=$((indent < max_indent ? indent : max_indent))
+        printf "%${indent}s↳%sin \`%s" '' "$source" "${FUNCNAME[frame]}"
+        if $extdebug; then
+            for ((i=argc_index-1; i >= argc_index-argc; i--)); do
+                printf " %s" "${BASH_ARGV[i]}"
+            done
+        fi
+        echo \'
+    done
+}
+errallow() {
+    if [[ $1 ]]; then
+        echo "errallow help: Undo the complimentary errcatch function."
+    else
+        set +E +o pipefail; trap ERR
+    fi
+}
+errcatch() {
+    set -E; shopt -s extdebug
+    _err-trap() {
+        err=$?
+        exec >&2
+        set +x
+        echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}:in \`$BASH_COMMAND' returned $err"
+        bash-trace 2
+        set -e
+        "${_errcatch_cleanup[@]}"
+        echo "$0: exiting with code $err"
+        exit $err
+    }
+    trap _err-trap ERR
+    set -o pipefail
+}
+errexit() {
+    exec >&2
+    code=1
+    if [[ $@ ]]; then
+        if [[ ${1/[^0-9]/} == "$1" ]]; then
+            code=$1
+            if [[ $2 ]]; then
+                echo "$2"
+            fi
+        else
+            echo "$0: $1"
+        fi
+    fi
+    echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}"
+    bash-trace 2
+    echo "$0: exiting with code $code"
+    exit $err
+}
+errcatch
diff --git a/gen-err b/gen-err
new file mode 100755 (executable)
index 0000000..236c3a7
--- /dev/null
+++ b/gen-err
@@ -0,0 +1,10 @@
+#!/bin/bash
+
+x="$(readlink -f "$BASH_SOURCE")"; cd ${x%/*} # directory of this file
+
+cat <<'EOF' >err
+#!/bin/bash
+# this file was generated from gen-err and meant to be sourced
+EOF
+sed -r '/^ *(#|$)/d' *-function >>err
+echo errcatch >>err