Files ending in -function are for sourcing then calling as a function. Files
without -function are exactly the same except they are for calling as a script.
-Patches and bugs are very welcome via gitlab.
-
-Questions, feedback, etc are very welcome via email to Ian Kelling
-<ian@iankelling.org>. I will add any useful questions, answers, etc. to this
-file. If a mailing list / forum is ever called for, I will start one.
+Patches, bugs, and any feedback is very welcome via gitorious or email to
+Ian Kelling <ian@iankelling.org>.
This program is also part of a collection of programs,
-https://gitlab.com/iankelling/bash-programs-by-ian, which are unrelated except
+https://gitorious.org/bash-programs-by-ian, which are unrelated except
having the same author and being being bash programs.
--- /dev/null
+#!/bin/bash
+# Copyright (C) 2015 Ian Kelling
+
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+shopt -s extdebug
+bash-trace() {
+ # shows function args when: shopt -s extdebug
+ local -i argc_index=0 arg 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
+}
if [[ $1 ]]; then
echo "errallow help: Undo the complimentary errcatch function."
else
- set +eE +o pipefail; trap ERR
+ set +E +o pipefail; trap ERR
fi
}
# See the License for the specific language governing permissions and
# limitations under the License.
+# on non-zero return codes outside of if/else,etc, print failure, stack trace, and exit
+# depends on bash-trace function
errcatch() {
- if [[ $1 ]]; then
- echo "errcatch help: Enable general purpose bash error handling.
-Exit on all errors and print useful information."
- else
- set -eE;
- trap 'echo "${BASH_COMMAND:+BASH_COMMAND=\"$BASH_COMMAND\" }
-${FUNCNAME:+FUNCNAME=\"$FUNCNAME\" }${LINENO:+LINENO=\"$LINENO\" }\$?=$?"' ERR
- set -o pipefail
- fi
+ set -E; shopt -s extdebug
+ _err-trap() {
+ err=$?
+ exec >&2
+ echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}:in \`$BASH_COMMAND' returned $err"
+ bash-trace 2
+ echo "$0: exiting with code $err"
+ exit $err
+ }
+ trap _err-trap ERR
+ set -o pipefail
}
--- /dev/null
+#!/bin/bash
+# Copyright (C) 2015 Ian Kelling
+
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+errexit() {
+ # usage: errexit [EXIT_CODE] [MESSAGE]
+ 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
+}