Bash skel
Jump to navigation
Jump to search
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 Options: -h show this help screen -x enable debug mode Example: $0 -x 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 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*) echo -e "Status ${SEV} - ${STR} ${OUT}" | tee -a ${OUT_FILE} ;; *INFO*) echo -e "Status ${SEV} - ${STR} ${OUT}" | tee -a ${OUT_FILE} ;; *ERR*) echo -e "Status ${SEV} - ${STR} ${OUT}" | tee -a ${OUT_FILE} exit 2 ;; FATAL*) echo -e "Status ${SEV} - ${STR} ${OUT}" | tee -a ${OUT_FILE} exit 2 ;; *WARN*) echo -e "Status ${SEV} - ${STR} ${OUT}" | tee -a ${OUT_FILE} exit 1 ;; *) echo -e "Status ${SEV} - ${STR} ${OUT}" | tee -a ${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} # old, buggy and too complex for no reason: #OUT_NAME=$(echo $0 | sed 's/\.sh/\txt/') #OUT_FILE="$(pwd)/out/${OUT_NAME}" #OUT_PATH=$(echo "${OUT_FILE}" | awk 'BEGIN{FS=OFS="/"}{NF--; print}') # Count the number of / chars to see if we really have a path #OUT_PTH="${OUT_FILE//[^\/]}" #if [[ ${#OUT_PTH} -ge 1 ]] && [[ ! -e ${OUT_PATH} ]] ; then # mkdir -p ${OUT_PATH} #fi #------------------------------------------- # Set our application defaults now #------------------------------------------- while getopts "hx" OPTION; do case ${OPTION} in h) usage; exit 0 ;; x) export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'; set -x ;; *) echo "Status FATAL - Unexpected argument given. Try -h, used $@"; exit 2 ;; esac done # Verify your env and command variables FIRST # if [[ -z ${SOMEVAR} ]]; then true ; else false ; 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