Last active
June 23, 2020 21:02
-
-
Save enjoylife/a298cf779a01953cece3cbf809481035 to your computer and use it in GitHub Desktop.
A stereotypical bash template with simple example patterns. Useful when creating scripts....
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Exit immediately for non zero status | |
set -e | |
# Check unset variables | |
set -u | |
# Print commands | |
set -x | |
# Prevent acciedently running as root | |
if [ "$(id -u)" -eq 0 ]; then | |
err "Don't run this script as root" | |
exit 1 | |
fi | |
# Simple way to be notified, | |
function bell { | |
echo -ne '\a' | |
} | |
function spacer { | |
printf "\n" | |
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' - | |
printf "\n" | |
} | |
function bell { | |
echo -ne '\a' | |
} | |
function ask { | |
while true; do | |
read -p "${1} [Y/n]" yn | |
case $yn in | |
[Yy]* ) make install; break;; | |
[Nn]* ) exit;; | |
* ) echo "Please answer yes or no.";; | |
esac | |
done | |
} | |
function err { | |
>&2 printf '\n\033[1;31m%s\033[0m\n' "$*" | |
} | |
function error_handler { | |
MYSELF="$0" | |
LASTLINE="$1" | |
bell | |
err | |
err | |
err "${MYSELF} failed at line ${LASTLINE}" | |
err | |
exit 1 | |
} | |
trap 'error_handler ${LINENO} ${$?}' ERR | |
function success { | |
>&2 printf '\n\033[1;32m%s\033[0m\n' "$*" | |
} | |
function warn { | |
>&2 printf '\n\033[1;33m%s\033[0m\n' "$*" | |
} | |
function info { | |
>&2 printf '\n\033[1;39m%s\033[0m\n' "$*" | |
} | |
function prompt { | |
if [ -z "${QUIET}" ]; then | |
bell | |
read -rp "$(echo -e '\n\033[1;33mPress [Enter] to continue or [Ctrl + C] to exit...\033[0m')" | |
echo | |
echo | |
fi | |
} | |
function with_backoff { | |
local max_attempts=${ATTEMPTS-5} | |
local timeout=${TIMEOUT-1} | |
local attempt=0 | |
local exitCode=0 | |
while [[ $attempt < $max_attempts ]] | |
do | |
"$@" | |
exitCode=$? | |
if [[ $exitCode == 0 ]] | |
then | |
break | |
fi | |
echo "Failure! Retrying in $timeout.." 1>&2 | |
sleep $timeout | |
attempt=$(( attempt + 1 )) | |
timeout=$(( timeout * 2 )) | |
done | |
if [[ $exitCode != 0 ]] | |
then | |
echo "You've failed me for the last time! ($@)" 1>&2 | |
fi | |
return $exitCode | |
} | |
function usage() { | |
echo "usage: | |
./template.sh -a <arg_a> -b <arg_b> -z <arg_z> | |
e.g. | |
./template.sh -a value_a -b value_b -z 42 | |
-a name of ah... | |
-b name of blah...; default is blah | |
-z answer to the universe; default is 42" | |
exit 1 | |
} | |
# commonly used local vars | |
WD=$(dirname "${0}") | |
WD=$(cd "${WD}" && pwd) | |
TIME="$(date '+%Y-%m-%d-%H-%M-%S')" | |
# Needing a temp dir but still want to clean up? Use this | |
TMP_DIR=$(mktemp -d -t template-dir) | |
trap 'rm -rf "${TMP_DIR}"' EXIT | |
# example to enumerate and put options into local vars and if something else is used, | |
# throw out the help message | |
while getopts a:b:z: arg ; do | |
case "${arg}" in | |
a) VAR_A_NAME="${OPTARG}";; | |
b) VAR_B_NAME="${OPTARG}";; | |
z) VAR_Z_NAME="${OPTARG}";; | |
*) usage;; | |
esac | |
done | |
# Conditional on exact value? | |
if [[ "${VAR_A_NAME}" = "value" ]]; then | |
# truthy | |
else | |
# falsy | |
fi | |
# Start some process which takes time to warmup, capturing the id and waiting on it for a few seconds | |
# then when our script exits we also clean up said running process | |
PROCESS_WAIT=5 | |
echo "Starting process..." | |
my_process.sh & PF_PID=${!} | |
# ensure port-forward is actually running | |
sleep "${PROCESS_WAIT}"; kill -0 "${PF_PID}" | |
trap 'kill ${PF_PID}' EXIT | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment