some light refactoring and general cleanup

This commit is contained in:
2025-06-30 12:04:16 -05:00
parent 781d438d4a
commit 363fd3eef3
5 changed files with 29 additions and 42 deletions

View File

@@ -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},$?] $ '
}
set -o functrace

5
includes/Logging.sh Executable file
View File

@@ -0,0 +1,5 @@
export CURRENT_TIMESTAMP
CURRENT_TIMESTAMP="$(date +%A-%Y-%m-%d-%T)"
export LOGFILENAME
LOGFILENAME="$0.${CURRENT_TIMESTAMP}.$$"

View File

@@ -1,8 +0,0 @@
#!/bin/bash
bail_out()
#Exit code
{
echo "Exiting...."
exit 0
}

View File

@@ -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