Skip to content

Instantly share code, notes, and snippets.

@iversond
Last active February 21, 2022 18:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iversond/0d3b1a22467b02c7ca4ed517a3d9e726 to your computer and use it in GitHub Desktop.
Save iversond/0d3b1a22467b02c7ca4ed517a3d9e726 to your computer and use it in GitHub Desktop.
Shell script template to use when creating new scripts. Borrows work from JR Bing's provision.sh in ps-vagabond
#!/usr/bin/env bash
# shellcheck disable=2059,2154,2034,2155,2046,2086
#===============================================================================
# vim: softtabstop=2 shiftwidth=2 expandtab fenc=utf-8 spelllang=en ft=sh
#===============================================================================
#
# FILE: shell_template.sh
#
# USAGE: ./shell_template.sh
#
# DESCRIPTION: Template to start other script scripts.
#
#===============================================================================
set -e # Exit immediately on error
set -u # Treat unset variables as an error
set -o pipefail # Prevent errors in a pipeline from being masked
IFS=$'\n\t' # Set the internal field separator to a tab and newline
###############
# Variables #
###############
#export DEBUG=true
declare -A timings
###############
# Functions #
###############
function echoinfo() {
local GC="\033[1;32m"
local EC="\033[0m"
printf "${GC} ☆ INFO${EC}: %s${GC}\n" "$@";
}
function echodebug() {
local BC="\033[1;34m"
local EC="\033[0m"
local GC="\033[1;32m"
if [[ -n ${DEBUG+x} ]]; then
printf "${BC} ★ DEBUG${EC}: %s${GC}\n" "$@";
fi
}
function echoerror() {
local RC="\033[1;31m"
local EC="\033[0m"
printf "${RC} ✖ ERROR${EC}: %s\n" "$@" 1>&2;
}
function display_timings_summary() {
local divider='=============================='
divider=$divider$divider
local header="\n %-28s %s\n"
local format=" %-28s %s\n"
local width=40
local total_duration=0
for duration in "${timings[@]}"; do
total_duration=$((duration + total_duration))
done
printf "$header" "TASK" "DURATION"
printf "%$width.${width}s\n" "$divider"
for key in "${!timings[@]}"; do
local converted_timing=$(date -u -d @${timings[$key]} +"%T")
printf "$format" "$key" "${converted_timing}"
done
printf "%$width.${width}s\n" "$divider"
printf "$format" "TOTAL TIME:" $(date -u -d @${total_duration} +"%T")
printf "\n"
}
# Sample function using Debug and adding Timings
# function execute_pre_setup() {
# local begin=$(date +%s)
# echoinfo "Executing Pre setup script"
# if [[ -n ${DEBUG+x} ]]; then
# if [ ! -z "${PSFT_CFG_DIR}" ]; then
# echodebug "Pre making PS_CFG_HOME"
# sudo mkdir -pv "${PSFT_CFG_DIR}"
# sudo chmod -v 777 "${PSFT_CFG_DIR}"
# else
# echodebug 'Skipping pre make PS_CFG_HOME, $PSFT_CFG_DIR not set.'
# fi
# else
# if [ ! -z "${PSFT_CFG_DIR}" ]; then
# sudo mkdir -p "${PSFT_CFG_DIR}" > /dev/null 2>&1
# sudo chmod 777 "${PSFT_CFG_DIR}" > /dev/null 2>&1
# fi
# fi
# local end=$(date +%s)
# local tottime="$((end - begin))"
# timings[execute_pre_setup]=$tottime
# }
########
# Main #
########
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment