Bash Script Skeleton

Getting Script Directory

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

Processing Arguments and Help

function help {
  echo "Usage: $0 <domain> <acme-string> [type] [name]"
  echo "Pass both type and name or neither."
  echo "Default: type=TXT and name=_acme-challenge"
  echo
  echo "E.g.:"
  echo "Setting a TXT record for _acme-challenge:"
}
 
if [ "$#" -lt 2 ]; then
  help
  exit 1
else
  domain=$1
  acme=$2
fi
if [ "$#" -eq 3 ]; then
  help
fi
if [ "$#" -eq 4 ]; then
  rtype=$3
  name=$4
else
  rtype="TXT"
  name="_acme-challenge"     # name of TXT record to update
fi

Setting Color

Ref: https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux

RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
CYAN='\033[1;36m'
NC='\033[0m' # No Color
printf "Setting ${YELLOW}$rtype${NC} record ${GREEN}$name${NC} with value ${RED}$acme${NC} for domain ${CYAN}$domain${NC} ...\n\n"

OLD - Check for number of arguments

#!/bin/bash
#
# .......... comment ............
#
NOW=$(date +"%Y-%m-%d-%H-%M-%S")
#
#
if [[ ! ("$#" == 1) ]]; then
  echo "One argument required."
  exit 1
fi
#
exit 0
#

Check if argument is empty

if [ -z "$1" ]; then
    echo 'Usage: $0 "<Comment>"'
    exit 1
fi

Sleep a random amount of time between 0 and 10 minutes

sleep $((RANDOM%600))

Check if file does not exist

if [ ! -f "$HOSTFILE" ]; then
  echo "File: $HOSTFILE does not exist"
  exit 1
fi

QR Code
QR Code tech:linux:bash_script_skeleton (generated for current page)