====== 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 [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
Related
echo "Total Number of Arguments:" $#
echo "Argument values:" $@
===== 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 ""'
exit 1
fi
===== if conditions =====
==== Single String Comparision ====
if [ $thesimple == 'N' ]; then
echo "Removing Cache"
sudo rm -Rf ${TARGET_MAIN}/frontend/web/assets/*
fi
==== OR condition ====
if [[ $thesimple == 'N' || $thegitit == 'N' ]]; then
echo "At least one of them is N"
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
===== Check if directory exist =====
if [ -d "$HOSTDIR" ]; then
echo "Directory: $HOSTDIR exists"
fi
===== Another Skeleton =====
Inspired by Nagios script
PROGNAME=`/usr/bin/basename $0`
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION="1.0"
#
STATE_OK=0
STATE_ERROR=1
#
check_root() {
# make sure script is running as non-root
if [ `whoami` == root ]; then
echo "Please make sure script is running as non-root."
exit $STATE_ERROR
fi
}
print_usage() {
echo "Usage: $PROGNAME [-s]"
echo " E.g.: $PROGNAME"
echo " E.g.: $PROGNAME -s"
echo "Usage: $PROGNAME --help"
echo "Usage: $PROGNAME --version"
echo ""
echo "Option -s is a Simple Deploy. No git check/commit."
echo "Option -a is to not exit on Apache check."
}
print_help() {
print_revision $PROGNAME $REVISION
echo ""
print_usage
echo ""
echo "Deploy Application to the Environment defined in deploy_settings"
echo ""
}
print_revision() {
echo "$1 v$2 Application Deployment"
}
# Check user is root
check_root
#
thesimple=N
theapache=N
thegitit=N
echo "Total Number of Arguments:" $#
echo "Argument values:" $@
while test -n "$1"; do
case "$1" in
--help)
print_help
exit $STATE_OK
;;
-h)
print_help
exit $STATE_OK
;;
--version)
print_revision $PROGNAME $REVISION
exit $STATE_OK
;;
-V)
print_revision $PROGNAME $REVISION
exit $STATE_OK
;;
--simple)
thesimple=Y
;;
-s)
thesimple=Y
;;
--apache)
theapache=Y
;;
-a)
theapache=Y
;;
--gitit)
thegitit=Y
;;
-g)
thegitit=Y
;;
--critical)
thecrit=$2
;;
-c)
thecrit=$2
;;
--fstype)
thefsty=$2
;;
-t)
thefsty=$2
;;
*)
echo "Unknown argument: $1"
print_usage
exit $STATE_ERROR
;;
esac
shift
done