From 363fd3eef3e180155930fc1f22879af7735f1e47 Mon Sep 17 00:00:00 2001 From: ReachableCEO Date: Mon, 30 Jun 2025 12:04:16 -0500 Subject: [PATCH] some light refactoring and general cleanup --- includes/{strictMode.sh => ErrorHandling.sh} | 35 ++++++++++++++------ includes/Logging.sh | 5 +++ includes/{pretty_print.sh => PrettyPrint.sh} | 0 includes/bail_out.sh | 8 ----- includes/error_out.sh | 23 ------------- 5 files changed, 29 insertions(+), 42 deletions(-) rename includes/{strictMode.sh => ErrorHandling.sh} (67%) create mode 100755 includes/Logging.sh rename includes/{pretty_print.sh => PrettyPrint.sh} (100%) delete mode 100644 includes/bail_out.sh delete mode 100644 includes/error_out.sh diff --git a/includes/strictMode.sh b/includes/ErrorHandling.sh similarity index 67% rename from includes/strictMode.sh rename to includes/ErrorHandling.sh index ba8159c..2d41042 100644 --- a/includes/strictMode.sh +++ b/includes/ErrorHandling.sh @@ -1,10 +1,6 @@ #!/bin/bash - -function StrictMode() -{ - -# Standard strict mode and error handling boilderplate.. +# Standard strict mode and error handling/tracing boilderplate.. # This is a function I include and execute in every shell script that I write. # It sets up a bunch of error handling odds and ends @@ -19,6 +15,28 @@ function StrictMode() #Here's the beef (as the commercial says..) +export PS4='(${BASH_SOURCE}:${LINENO}): - [${SHLVL},${BASH_SUBSHELL},$?] $ ' + +function error_out() +{ + echo "Bailing out. See above for reason...." + exit 1 +} + +function handle_failure() { + local lineno=$1 + local fn=$2 + local exitstatus=$3 + local msg=$4 + local lineno_fns=${0% 0} + if [[ "$lineno_fns" != "-1" ]] ; then + lineno="${lineno} ${lineno_fns}" + fi + echo "${BASH_SOURCE[0]}: Function: ${fn} Line Number : [${lineno}] Failed with status ${exitstatus}: $msg" +} + +trap 'handle_failure "${BASH_LINENO[*]}" "$LINENO" "${FUNCNAME[*]:-script}" "$?" "$BASH_COMMAND"' ERR + #use errexit (a.k.a. set -e) to make your script exit when a command fails. #add || true to commands that you allow to fail. set -o errexit @@ -33,9 +51,4 @@ set -o nounset set -o pipefail #Function tracing... -set -o functrace - - -export PS4='(${BASH_SOURCE}:${LINENO}): - [${SHLVL},${BASH_SUBSHELL},$?] $ ' - -} \ No newline at end of file +set -o functrace \ No newline at end of file diff --git a/includes/Logging.sh b/includes/Logging.sh new file mode 100755 index 0000000..b168817 --- /dev/null +++ b/includes/Logging.sh @@ -0,0 +1,5 @@ +export CURRENT_TIMESTAMP +CURRENT_TIMESTAMP="$(date +%A-%Y-%m-%d-%T)" + +export LOGFILENAME +LOGFILENAME="$0.${CURRENT_TIMESTAMP}.$$" \ No newline at end of file diff --git a/includes/pretty_print.sh b/includes/PrettyPrint.sh similarity index 100% rename from includes/pretty_print.sh rename to includes/PrettyPrint.sh diff --git a/includes/bail_out.sh b/includes/bail_out.sh deleted file mode 100644 index cae8d46..0000000 --- a/includes/bail_out.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash - -bail_out() -#Exit code -{ -echo "Exiting...." -exit 0 -} diff --git a/includes/error_out.sh b/includes/error_out.sh deleted file mode 100644 index 45764af..0000000 --- a/includes/error_out.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/bash - - -function error_out() -{ - echo "Bailing out. See above for reason...." - exit 1 -} - - -function handle_failure() { - local lineno=$1 - local fn=$2 - local exitstatus=$3 - local msg=$4 - local lineno_fns=${0% 0} - if [[ "$lineno_fns" != "-1" ]] ; then - lineno="${lineno} ${lineno_fns}" - fi - echo "${BASH_SOURCE[0]}: Function: ${fn} Line Number : [${lineno}] Failed with status ${exitstatus}: $msg" -} - -trap 'handle_failure "${BASH_LINENO[*]}" "$LINENO" "${FUNCNAME[*]:-script}" "$?" "$BASH_COMMAND"' ERR