Differences

This shows you the differences between two versions of the page.

Link to this comparison view

tech:linux:bash_script_skeleton [2019/07/22 06:28]
tech:linux:bash_script_skeleton [2022/12/26 06:29] (current)
Line 1: Line 1:
 +====== Bash Script Skeleton ======
 +===== Getting Script Directory =====
 +<code bash>
 +SCRIPT_DIR=$( cd -- "$( dirname -- "​${BASH_SOURCE[0]}"​ )" &> /dev/null && pwd )
 +</​code>​
 +
 +===== Processing Arguments and Help =====
 +<code bash>
 +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
 +</​code>​
 +Related
 +<code bash>
 +echo "Total Number of Arguments:"​ $#
 +echo "​Argument values:"​ $@
 +</​code>​
 +===== Setting Color =====
 +Ref: https://​stackoverflow.com/​questions/​5947742/​how-to-change-the-output-color-of-echo-in-linux
 +
 +<​code>​
 +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"​
 +</​code>​
 +
 +===== OLD - Check for number of arguments =====
 +<code bash>
 +#!/bin/bash
 +#
 +# .......... comment ............
 +#
 +NOW=$(date +"​%Y-%m-%d-%H-%M-%S"​)
 +#
 +#
 +if [[ ! ("​$#"​ == 1) ]]; then
 +  echo "One argument required."​
 +  exit 1
 +fi
 +#
 +exit 0
 +#
 +</​code>​
 +
 +===== Check if argument is empty =====
 +<code bash>
 +if [ -z "​$1"​ ]; then
 +    echo '​Usage:​ $0 "<​Comment>"'​
 +    exit 1
 +fi
 +</​code>​
 +
 +===== if conditions =====
 +==== Single String Comparision ====
 +<code bash>
 +if [ $thesimple == '​N'​ ]; then
 +  echo "​Removing Cache"
 +  sudo rm -Rf ${TARGET_MAIN}/​frontend/​web/​assets/​*
 +fi
 +</​code>​
 +
 +==== OR condition ====
 +<code bash>
 +if [[ $thesimple == '​N'​ || $thegitit ​ == '​N'​ ]]; then
 +    echo "At least one of them is N"
 +fi
 +</​code>​
 +
 +===== Sleep a random amount of time between 0 and 10 minutes =====
 +<code bash>
 +sleep $((RANDOM%600))
 +</​code>​
 +
 +===== Check if file does not exist =====
 +<code bash>
 +if [ ! -f "​$HOSTFILE"​ ]; then
 +  echo "File: $HOSTFILE does not exist"
 +  exit 1
 +fi
 +</​code>​
 +===== Check if directory exist =====
 +<code bash>
 +if [ -d "​$HOSTDIR"​ ]; then
 +  echo "​Directory:​ $HOSTDIR exists"​
 +fi
 +</​code>​
 +
 +===== Another Skeleton =====
 +Inspired by Nagios script
 +<code bash>
 +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
 +</​code>​
  

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