Bash skel: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
|||
| (2 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
<metadesc>Bash skeleton to use when making scripts</metadesc> | |||
==== Basic Skeleton to build scripts from ==== | ==== Basic Skeleton to build scripts from ==== | ||
<pre> | <pre> | ||
| Line 22: | Line 23: | ||
Usage: $0 options | Usage: $0 options | ||
Basic summary of what this file does | |||
Options: | Options: | ||
-h show this help screen | -h show this help screen | ||
-x enable debug mode | -x enable debug mode | ||
-v set logging to verbose mode | |||
Example: | Example: | ||
$0 -x | $0 -x | ||
$0 -x -y | |||
EOF | EOF | ||
| Line 48: | Line 53: | ||
STR="No details given" | STR="No details given" | ||
fi | fi | ||
# Set any details we recieved here | |||
if [[ -z $3 ]]; then | if [[ -z $3 ]]; then | ||
OUT=' | OUT='' | ||
else | |||
OUT="\n${3}" | |||
fi | fi | ||
| Line 62: | Line 71: | ||
# Set our output now into our "logfile" | # Set our output now into our "logfile" | ||
case ${SEV} in | case ${SEV} in | ||
DEBUG*) | |||
if [[ ${VERBOSE} = 'true' ]]; then | |||
echo -e "\033[0;36mStatus ${SEV} - ${STR}\033[1;36m${OUT}" | |||
fi | |||
;; | |||
INFO*) echo -e "\033[1;34mStatus ${SEV} - ${STR} ${OUT}" ;; | |||
CAUT*) echo -e "\033[1;35mStatus ${SEV} - ${STR} ${OUT}" ; | |||
echo -e "Status ${SEV} - ${STR} ${OUT}" >> ${OUT_FILE} ;; | |||
ERRO*) echo -e "\033[1;31mStatus ${SEV} - ${STR} ${OUT}" ; | |||
echo -e "Status ${SEV} - ${STR} ${OUT}" >> ${OUT_FILE} ; exit 2 ;; | |||
FATA*) echo -e "\033[0;31mStatus ${SEV} - ${STR} ${OUT}"; | |||
echo -e "Status ${SEV} - ${STR} ${OUT}" >> ${OUT_FILE} ; exit 2 ;; | |||
CRIT*) echo -e "\033[0;31mStatus ${SEV} - ${STR} ${OUT}"; | |||
echo -e "Status ${SEV} - ${STR} ${OUT}" >> ${OUT_FILE} ; exit 2 ;; | |||
WARN*) echo -e "\033[0;33mStatus ${SEV} - ${STR} ${OUT}" ;; | |||
OK) echo -e "\033[0;32mStatus ${SEV} - ${STR} ${OUT}" ;; | |||
*) echo -e "\033[0;32mStatus ${SEV} - ${STR} ${OUT}"; | |||
echo -e "Status ${SEV} - ${STR} ${OUT}" >> ${OUT_FILE} ;; | |||
esac | esac | ||
# Unset the death by exit codes now | # Unset the death by exit codes now | ||
| Line 118: | Line 136: | ||
# values are even added | # values are even added | ||
#-------------------------------------------- | #-------------------------------------------- | ||
# configure the logging system | # configure the logging system | ||
FIL=$(echo "$0" | awk -F '/' '{print $NF}' | sed 's/\.sh/\.txt/') | FIL=$(echo "$0" | awk -F '/' '{print $NF}' | sed 's/\.sh/\.txt/') | ||
OUT_FILE=$(pwd)/out/${FIL} ; mkdir -p out ; touch ${OUT_FILE} | OUT_FILE=$(pwd)/out/${FIL} ; mkdir -p out ; touch ${OUT_FILE} | ||
WORK_DIR=$(echo "$(pwd)/$0" | sed 's/\(.*\)\/.*/\1/') | |||
#------------------------------------------- | #------------------------------------------- | ||
# Set our application defaults now | # Set our application defaults now | ||
#------------------------------------------- | #------------------------------------------- | ||
DEFAULT_VALUE="test01" | |||
ANOTHER_DEFAULT="tesst02" | |||
SOMEVAR='true' | |||
VERBOSE='false' | |||
while getopts " | while getopts ":hxv" OPTION; do | ||
case ${OPTION} in | case ${OPTION} in | ||
h) usage; exit 0 ;; | h) usage; exit 0 ;; | ||
x) export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'; set -x ;; | x) export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'; set -x ;; | ||
v) VERBOSE='true' ;; | |||
\?) echo "ERROR: Unknown option: -${OPTARG} Use -h for help." >&2 ; exit 2 ;; | |||
:) echo "ERROR: Option -${OPTARG} requires an argument. Use -h for help." >&2 ; exit 2 ;; | |||
esac | esac | ||
done | done | ||
# Verify your env and command variables FIRST | # Verify your env and command variables FIRST | ||
if [[ -z ${SOMEVAR} ]]; then | |||
logger "INFO" "Variable \${SOMEVAR} is not set" | |||
else | |||
logger "OK" "Variable \${SOMEVAR} is set" | |||
fi | |||
# Install packages | # Install packages | ||
Latest revision as of 13:53, 21 October 2026
Basic Skeleton to build scripts from
#!/bin/bash -
#===============================================================================
#
# FILE: filename.sh
# USAGE: ./filename.sh options (if defined, see help)
# AUTHOR: Chris Hubbard (CSH), chubbard@iwillfearnoevil.com
# ORGANIZATION: I Will Fear No Evil
# CREATED: 09/12/2014 11:30:20
# REVISION: Amanda
#===============================================================================
# Treat unset variables as an error (disabled by default)
# set -o nounset
#----------------------------------------
# Always define a help system by default
#----------------------------------------
usage() {
cat << EOF
Usage: $0 options
Basic summary of what this file does
Options:
-h show this help screen
-x enable debug mode
-v set logging to verbose mode
Example:
$0 -x
$0 -x -y
EOF
}
#--------------------------------------
# Function General logger system
#--------------------------------------
logger() {
# This is the ONLY place that should exit out of our
# script with an error as the -e is not set in shopt values
SEV=$1
STR=$2
OUT=$3
# Honor exit codes HERE only
set -e
if [[ -z $2 ]]; then
STR="No details given"
fi
# Set any details we recieved here
if [[ -z $3 ]]; then
OUT=''
else
OUT="\n${3}"
fi
# If the file does not exist, simply touch it
if [[ ! -e ${OUT_FILE} ]]; then
touch ${OUT_FILE}
fi
# Make sure our output is uniform. Why look like a sloppy coder?
SEV=$(echo "${SEV}" | tr [:lower:] [:upper:])
# Set our output now into our "logfile"
case ${SEV} in
DEBUG*)
if [[ ${VERBOSE} = 'true' ]]; then
echo -e "\033[0;36mStatus ${SEV} - ${STR}\033[1;36m${OUT}"
fi
;;
INFO*) echo -e "\033[1;34mStatus ${SEV} - ${STR} ${OUT}" ;;
CAUT*) echo -e "\033[1;35mStatus ${SEV} - ${STR} ${OUT}" ;
echo -e "Status ${SEV} - ${STR} ${OUT}" >> ${OUT_FILE} ;;
ERRO*) echo -e "\033[1;31mStatus ${SEV} - ${STR} ${OUT}" ;
echo -e "Status ${SEV} - ${STR} ${OUT}" >> ${OUT_FILE} ; exit 2 ;;
FATA*) echo -e "\033[0;31mStatus ${SEV} - ${STR} ${OUT}";
echo -e "Status ${SEV} - ${STR} ${OUT}" >> ${OUT_FILE} ; exit 2 ;;
CRIT*) echo -e "\033[0;31mStatus ${SEV} - ${STR} ${OUT}";
echo -e "Status ${SEV} - ${STR} ${OUT}" >> ${OUT_FILE} ; exit 2 ;;
WARN*) echo -e "\033[0;33mStatus ${SEV} - ${STR} ${OUT}" ;;
OK) echo -e "\033[0;32mStatus ${SEV} - ${STR} ${OUT}" ;;
*) echo -e "\033[0;32mStatus ${SEV} - ${STR} ${OUT}";
echo -e "Status ${SEV} - ${STR} ${OUT}" >> ${OUT_FILE} ;;
esac
# Unset the death by exit codes now
set +e
}
#---------------------------------------
# Function to make sure we can find all binaries
#---------------------------------------
verifyDeps() {
# Dont even bother running if we do not have the binaries that we need
needed="grep sed ssh jq"
for i in ${needed} ; do
type $i >/dev/null 2>&1
if [[ $? -eq 1 ]]; then
logger "FATAL" "Missing manditory component: $i"
fi
done
}
#--------------------------------------
# Function to install specific packages
# call before verify_deps
#--------------------------------------
installPackage() {
# Install all manditory packages here
export DEBIAN_FRONTEND=noninteractive
export DEBCONF_NONINTERACTIVE_SEEN=true
local aptUpdate=$( apt update 2> /dev/null)
local aptInstall=$( apt install -y jq 2> /dev/null)
if [[ $? -ne 0 ]]; then
logger "FATAL" "Unable to install manditory packages" "${aptUpdate}${aptInstall}"
else
logger "INFO" "Installed manditory package(s)"
fi
}
#--------------------------------------------
# Function to do something useful
#--------------------------------------------
doSomething() {
echo "Doing something useful"
}
#--------------------------------------------
# boilerplate for the script before application specific
# values are even added
#--------------------------------------------
# configure the logging system
FIL=$(echo "$0" | awk -F '/' '{print $NF}' | sed 's/\.sh/\.txt/')
OUT_FILE=$(pwd)/out/${FIL} ; mkdir -p out ; touch ${OUT_FILE}
WORK_DIR=$(echo "$(pwd)/$0" | sed 's/\(.*\)\/.*/\1/')
#-------------------------------------------
# Set our application defaults now
#-------------------------------------------
DEFAULT_VALUE="test01"
ANOTHER_DEFAULT="tesst02"
SOMEVAR='true'
VERBOSE='false'
while getopts ":hxv" OPTION; do
case ${OPTION} in
h) usage; exit 0 ;;
x) export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'; set -x ;;
v) VERBOSE='true' ;;
\?) echo "ERROR: Unknown option: -${OPTARG} Use -h for help." >&2 ; exit 2 ;;
:) echo "ERROR: Option -${OPTARG} requires an argument. Use -h for help." >&2 ; exit 2 ;;
esac
done
# Verify your env and command variables FIRST
if [[ -z ${SOMEVAR} ]]; then
logger "INFO" "Variable \${SOMEVAR} is not set"
else
logger "OK" "Variable \${SOMEVAR} is set"
fi
# Install packages
installPakcage
# Confirm binaries exist
verifyDeps
# Do something useful
doSomething
# Always call out that we are done
logger "INFO" "Script $0 complete"
exit 0