re-factoring into my shell script framework.

shifting away from invoking via curl and using a downloaded zip file or git clone.
This commit is contained in:
2025-06-30 13:07:25 -05:00
parent d64d75cb3b
commit d82c8733fa
66 changed files with 194 additions and 0 deletions

View File

@ -0,0 +1,4 @@
#Global Variables used by the framework
export ProjectIncludes="0"
export PreflightCheck="0"

View File

@ -0,0 +1,19 @@
#Place (primary/unique) key as very first argument on each record below
#Value Fields are (in order), (reference KEY_(VARIABLE) names in the code):
#1)valuex (description)
#2)valuey (description)
#3)valuez (description)
#An example:
#unique key of serverfqdn
#key of: subnet , value of: 10.10.10.0/24
#key of: gateway, value of: 10.10.10.1
#serverfqdn,10.10.10.0/24,10.10.10.1
#Place your records below:
#primary/uniquekey,#value
primarykey1,valuex,valuey,valuez
primarykey2,valuex,valuey,valuez

View File

@ -0,0 +1,33 @@
function DebugMe() {
[[ $script_debug = 1 ]] && "$@" || :
#to turn debugging on, set script_debug=1
#to turn debugging off, set script_debug=0
# be sure to append || : or || true here or use return 0, since the return code
# of this function should always be 0 to not influence anything else with an unwanted
# "false" return code (for example the script's exit code if this function is used
# as the very last command in the script)
#This function does nothing when script_debug is unset or empty, but it executes the
#given parameters as commands when script_debug is set. Use it like this:
#debugme logger "Sorting the database"
#database_sort
#debugme logger "Finished sorting the database, exit code $?"
# * print commands to be executed to stderr as if they were read from input
# (script file or keyboard)
# * print everything before any ( substitution and expansion, …) is applied
set -v
# * print everything as if it were executed, after substitution and expansion is applied
# * indicate the depth-level of the subshell (by default by prefixing a + (plus) sign to
# the displayed command)
# * indicate the recognized words after word splitting by marking them like 'x y'
# * in shell version 4.1, this debug output can be printed to a configurable file
#descriptor, rather than sdtout by setting the BASH_XTRACEFD variable.
set -x
}

View File

@ -0,0 +1,54 @@
#!/bin/bash
# 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
# Bits and pieces Sourced from (as best I recall):
# * https://news.ycombinator.com/item?id=24727495
# * many other hacker news / slashdot etc posts over the years
# * https://www.tothenew.com/blog/foolproof-your-bash-script-some-best-practices/
# * https://translucentcomputing.com/2020/05/unofficial-bash-strict-mode-errexit/
# * http://redsymbol.net/articles/unofficial-bash-strict-mode/
# * the school of hard knocks... (aka my code failures...)
#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
# Use set -o nounset (a.k.a. set -u) to exit when your script tries to use undeclared
# variables.
set -o nounset
#Use set -o pipefail in scripts to catch (for example) mysqldump fails
#in e.g. mysqldump |gzip.
#The exit status of the last command that threw a non-zero exit code is returned
set -o pipefail
#Function tracing...
set -o functrace

View File

@ -0,0 +1,13 @@
#!/bin/bash
function LocalHelp()
{
echo "$0 is <description here>"
echo "$0 takes <num> arguments: "
echo "1) <stuff>"
echo "2) <other stuff>"
echo "<additional info on arguments...>:"
echo "<put>"
echo "<stuff>"
echo "<here>"
}

5
Framework-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

@ -0,0 +1,15 @@
#!/bin/bash
function LookupKV()
{
echo "KV lookup..."
#Arguments:
#$1 <path to key/value table>
#$2 unique record identifier
#Returns:
#Variable/array containing all the values in the record
}

View File

@ -0,0 +1,31 @@
#!/bin/bash
function PreflightCheck()
{
#Common things I check for in the scripts I write.
#export curr_host="$(hostname)"
#export curr_user="$USER"
#export host_check="$(echo $curr_host | grep -c <desired hostname>)"
#export user_check="$(echo $curr_user | grep -c <desired username>)"
#if [ $host_check -ne 1 ]; then
# echo "Must run on <desired host>."
# error_out
#fi
#if [ $user_check -ne 1 ]; then
# echo "Must run as <desired user>."
# error_out
#fi
#if [ "$ARG_COUNT" -ne <the right num> ]; then
# help
# error_out
#fi
#Your additional stuff here...
echo "All checks passed...."
}

View File

@ -0,0 +1,20 @@
function print_info()
{
GREEN='\033[0;32m'
NC='\033[0m'
tput bold
echo -e "$GREEN $1${NC}"
echo -e "$GREEN $1${NC}" >> "$LOGFILENAME"
tput sgr0
}
function print_error()
{
RED='\033[0;31m'
NC='\033[0m'
tput bold
echo -e "$RED $1${NC}"
echo -e "$RED $1${NC}" >> "$LOGFILENAME"
echo "$1"
tput sgr0
}