Last active
July 17, 2025 14:49
-
-
Save danshardware/0560bba47c9b9d64523fc75f6784831a to your computer and use it in GitHub Desktop.
Dan's Shell Script Template
This file contains hidden or 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 | |
| ##### | |
| # Dan's shell script template v1.0 | |
| # Public Domain. Use and abuse to your heart's delight | |
| # Set some basic shell parameters. Let's list some useful ones: | |
| # a - Auto export all variables created. This shares them with sub processes without the 'export' built-in | |
| # e - Die on errors | |
| # u - Die if unset variables are used | |
| # x - Trace (print) commands as they are executed | |
| # o - Set Bash options: | |
| # pipefail - only consider a pipe failed if the last expression fails | |
| # posix - Change behavior to match posix when there are differences | |
| set -eo pipefail | |
| # if verbose is set, then we set "Print all commands before execution" | |
| if [[ -n ${VERBOSE+x} ]]; then | |
| set -x | |
| fi | |
| # Set Defaults. The syntax is VAR_NAME=${VAR_NAME:-default_value} | |
| #default to us-east-1 | |
| export AWS_REGION=${AWS_REGION:-us-east-1} | |
| # a few colors to be cool | |
| GREEN="\033[32m" | |
| REG="\033[0m" | |
| RED="\033[31m" | |
| YELLOW="\033[33m" | |
| WHITE="\033[1;37m" | |
| SUBDUED="\033[90m" | |
| if [[ "$TERM" != *color* ]]; then | |
| # no pretty colors if this is a non-color terminal | |
| GREEN="" | |
| REG="" | |
| RED="" | |
| YELLOW="" | |
| WHITE="" | |
| SUBDUED="" | |
| fi | |
| # A few helper variables | |
| ME=$(basename $0) | |
| DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
| error(){ | |
| echo -e "[${RED}ERROR${REG}] $1" 1>&2 | |
| } | |
| warn(){ | |
| echo -e "[${YELLOW}WARNING${REG}] $1" 1>&2 | |
| } | |
| usage(){ | |
| echo -e "${WHITE}$ME${REG} - what I do | |
| ${SUBDUED}Usage:${REG} $ME explain | |
| ${WHITE}parameter${REG}\texplaination | |
| Notes: | |
| Any notes | |
| " | |
| } | |
| # Handle simple options | |
| while getopts ":v" o; do | |
| case "${o}" in | |
| v) | |
| set -x | |
| VERBOSE=1 | |
| ;; | |
| *) | |
| usage | |
| ;; | |
| esac | |
| done | |
| shift $((OPTIND-1)) | |
| # Handle positional variables | |
| # ensure we have the required number of parameters | |
| if [[ "$#" -lt 2 ]]; then | |
| usage | |
| exit 2 | |
| fi | |
| PARAM="$1" | |
| shift | |
| # Check that all our sub-commands exist | |
| for command in jq; do | |
| command -v "${command}" 2>&1 > /dev/null || \ | |
| { error "${WHITE}The '${command}' command is not found${REG}. Please install it and ensure it's in the PATH" ; ALLGOOD=bad; }; | |
| done | |
| # Check if we had any missing commands | |
| if [[ "${ALLGOOD}" == "bad" ]]; then | |
| error "Errors encounterd. Please see above and try again." | |
| exit 1 | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment