now in it's own repo, like it should be!
This commit is contained in:
parent
665b1b8d85
commit
dcb40d94ab
11
README.md
11
README.md
@ -1,3 +1,12 @@
|
||||
# ReachableCEOShellFramework
|
||||
|
||||
My shell scripting framework developed over the years
|
||||
## Introduction
|
||||
|
||||
My shell scripting framework developed over 20 years of coding bash professionally.
|
||||
|
||||
This is a collection of code/functions/templates/methodologies I've put together over 20 years of coding.
|
||||
|
||||
* Error handling/tracing
|
||||
* Help
|
||||
* Robust CLI argument handling
|
||||
|
||||
|
12
README_template.md
Normal file
12
README_template.md
Normal file
@ -0,0 +1,12 @@
|
||||
#<project name>
|
||||
|
||||
|
||||
## Introduction
|
||||
|
||||
This is a project.It does stuff..
|
||||
|
||||
## Assumptions
|
||||
|
||||
## Requirements
|
||||
|
||||
## Dependencies
|
33
includes/DebugMe.sh
Normal file
33
includes/DebugMe.sh
Normal 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
|
||||
|
||||
}
|
13
includes/LocalHelp.sh
Normal file
13
includes/LocalHelp.sh
Normal 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>"
|
||||
}
|
13
includes/LookupKv.sh
Normal file
13
includes/LookupKv.sh
Normal file
@ -0,0 +1,13 @@
|
||||
#!/bin/bash
|
||||
|
||||
function LookupKV()
|
||||
{
|
||||
|
||||
#Arguments:
|
||||
#$1 <path to key/value table>
|
||||
#$2 unique record identifier
|
||||
|
||||
#Returns:
|
||||
#Variable/array containing all the values in the record
|
||||
|
||||
}
|
31
includes/PreflightCheck.sh
Normal file
31
includes/PreflightCheck.sh
Normal 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...."
|
||||
|
||||
}
|
8
includes/bail_out.sh
Normal file
8
includes/bail_out.sh
Normal file
@ -0,0 +1,8 @@
|
||||
#!/bin/bash
|
||||
|
||||
bail_out()
|
||||
#Exit code
|
||||
{
|
||||
echo "Exiting...."
|
||||
exit 0
|
||||
}
|
23
includes/error_out.sh
Normal file
23
includes/error_out.sh
Normal file
@ -0,0 +1,23 @@
|
||||
#!/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
|
41
includes/strictMode.sh
Normal file
41
includes/strictMode.sh
Normal file
@ -0,0 +1,41 @@
|
||||
#!/bin/bash
|
||||
|
||||
|
||||
function StrictMode()
|
||||
{
|
||||
|
||||
# Standard strict mode and error handling 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..)
|
||||
|
||||
#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
|
||||
|
||||
|
||||
export PS4='(${BASH_SOURCE}:${LINENO}): - [${SHLVL},${BASH_SUBSHELL},$?] $ '
|
||||
|
||||
}
|
66
project.sh
Normal file
66
project.sh
Normal file
@ -0,0 +1,66 @@
|
||||
#!/bin/bash
|
||||
|
||||
#Framework variables are read from hee
|
||||
source vars/FrameworkVars
|
||||
|
||||
#Boilerplate and support functions
|
||||
FrameworkIncludeFiles="$(ls -1 --color=none includes/*)"
|
||||
|
||||
IFS=$'\n\t'
|
||||
for file in ${FrameworkIncludeFiles[@]}; do
|
||||
. "$file"
|
||||
done
|
||||
unset IFS
|
||||
|
||||
|
||||
if [[ ProjectIncludes = 1 ]]; then
|
||||
ProjectIncludeFiles="$(ls -1 --color=none project-includes/*)"
|
||||
IFS=$'\n\t'
|
||||
for file in ${ProjectIncludeFiles[@]}; do
|
||||
. "$file"
|
||||
done
|
||||
unset IFS
|
||||
fi
|
||||
|
||||
|
||||
#####
|
||||
#Core framework functions...
|
||||
#####
|
||||
|
||||
|
||||
while [ ! -z "$1" ];do
|
||||
case "$1" in
|
||||
-h|--help)
|
||||
LocalHelp
|
||||
;;
|
||||
-k1|--key1)
|
||||
shift
|
||||
KEY1="$1"
|
||||
echo "key 1 is $KEY1"
|
||||
;;
|
||||
-k2|--key2)
|
||||
shift
|
||||
KEY2="$1"
|
||||
echo "key 2 is $KEY2"
|
||||
;;
|
||||
*)
|
||||
echo "Displaying $0 help..."
|
||||
LocalHelp
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
|
||||
function main()
|
||||
{
|
||||
StrictMode
|
||||
|
||||
if [ PreflightCheck = 1 ]; then
|
||||
PreflightCheck
|
||||
fi
|
||||
|
||||
#Your custom logic here....
|
||||
echo "Custom logic here..."
|
||||
}
|
||||
|
||||
main
|
5
vars/FrameworkVars
Normal file
5
vars/FrameworkVars
Normal file
@ -0,0 +1,5 @@
|
||||
#Global Variables used by the framework
|
||||
|
||||
export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
|
||||
export ProjectIncludes="0"
|
||||
export PreflightCheck="0"
|
19
vars/kv_map
Normal file
19
vars/kv_map
Normal 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
|
Loading…
Reference in New Issue
Block a user