Notes‎ > ‎

Bash Scripting

Sample template of a bash style script

#!/usr/bin/env bash
# #############################################################################

        NAME_="template"
     PURPOSE_=""
    SYNOPSIS_="${NAME_} [-options] <param>"
    REQUIRES_=""
     VERSION_="1.2"
        DATE_="2007.05.15; last update: 2007.10.26"
      AUTHOR_="James H. Nguyen <james [at] bluecentre [dot] net>"
         URL_="james.bluecentre.net"
    CATEGORY_=""
    PLATFORM_=""
       SHELL_="bash"
  DISTRIBUTE_="yes"

# #############################################################################
# This program is distributed under the terms of the GNU General Public License

# HISTORY:
# 2007.10.04 v1.1 - changed the shebang to something that will run on a more
#                   variety of systems and distributions.
# 2007.10.24 v1.2 - added non-interactive mode for use in scripting that does
#                   not output fancy colours

# #############################################################################
# BEGIN: Declaration of system commands
LS=ls
SSH=ssh
SED=sed
AWK=awk
ECHO=/usr/bin/echo
GREP=grep
PRINTF=printf
NETG=netg
LDAPSEARCH=ldapsearch
# END: Declaration of system commands
# #############################################################################


# #############################################################################
# BEGIN: Declaration of system paths
# END: Declaration of system paths
# #############################################################################


# #############################################################################
# BEGIN: Declaration of script variables
# Making Scripts Colorful
#     * tput sgr0 #Reset text attributes to normal without clearing screen
#     * Escape sequence to change colors 
# #\e[${forground};${background}m 
# example white on black sequence is \e[37;40m 
# #Forground colors Backgrounfd Color 
# black=30; bgblace=40 
# red=31; bgred=41 
# green=32; bggreen=42 
# yellow=33; bgyellow=43 
# blue=34; bgblue=44 
# magenta=35; bgmagenta=45 
# cyan=36; bgcyan=46 
# white=37; bgwhite=47
#     * Turn text attribute off \e[0m
#     * Change text attribute bold \e[1m
#     * Change text attribute underline \e[4m
#     * Change text attributer everse \e[7m 
GREEN="\033[00;32m"
YELLOW="\033[01;33m"
RED="\033[00;31m"
NO_COLOUR="\033[00m"

SSH_CONDITION="[CONDITION] > /dev/null 2>&1 && echo '[${GREEN}TRUE_MSG${NO_COLOUR}]' \`hostname\` || echo '[${YELLOW}FALSE_MSG${NO_COLOUR}]' \`hostname\`"
T_MSG='OK'
F_MSG='!!'

INTERACTIVE=y
# END: Declaration of script variables
# #############################################################################


# #############################################################################
# BEGIN: Declaration of functions
usage() {
${PRINTF} >&2 "${NAME_} ${VERSION_} - ${PURPOSE_}
Usage: ${SYNOPSIS_}
Requires: ${REQUIRES_}
Options:
-i y|n, interactive switch
-a,     description
-b,     description
-c,     description
-h,     displays usage\n"
exit 1
}

# @param1 return code from $?
# @param2 message to display
# @usage checkErrors $? "msg"
checkErrors() {
if [ "${1}" -ne "0" ]; then
${PRINTF} "[${RED}!!${NO_COLOUR}] #${1}: ${2}\n"
exit ${1}
fi
}

# @param1 message to display
info() {
if [ ${INTERACTIVE} = "y" ]
then
${ECHO} ${GREEN}${1}${NO_COLOUR}
else
${ECHO} ${1}
fi
}

# @param1 message to display
warn() {
if [ ${INTERACTIVE} = "y" ]
then
${ECHO} ${YELLOW}${1}${NO_COLOUR}
else
${ECHO} ${1}
fi
}

# @param1 message to display
error() {
if [ ${INTERACTIVE} = "y" ]
then
${ECHO} ${RED}${1}${NO_COLOUR}
else
${ECHO} ${1}
fi
}

functionA() {
info 'this is an info'
return 0
}

functionB() {
warn 'this is a warning'
return 0
}

functionC() {
error 'this is an error'
`blah`
checkErrors $? 'testing checkErrors function'
return 0
}

functionD() {
CONDITION_CMD='ps -ef | grep james'
SSH_CMD=`${ECHO} ${SSH_CONDITION} | ${SED} -e "s/\[CONDITION\]/${CONDITION_CMD}/" -e "s/TRUE_MSG/${T_MSG}/" -e "s/FALSE_MSG/${F_MSG}/"`
${SSH} rodan "${SSH_CMD}"

checkErrors $? "command failed"
return 0
}
# END: Declaration of functions
# #############################################################################


# #############################################################################
# BEGIN: Main script
# #
# Special Variables
#
#    * $? Exit status or return code of last command
#    * $# Number of arguments
#    * $@ Argument 1 thru n with Input Field Separator
#    * $* "$1" $2" ... $n
#    * $! Process id of last background process
#    * $$ Process id of shell running this script
#    * $- The current shell flags 

#if [ $# -eq 0 ] || [ $# -eq 1 ]; then
#if [ $# -le 0 ]; then
# usage       
# exit 1
#fi

# uses bash function getopts
while getopts ":i:" options; do
case ${options} in
i )
INTERACTIVE=${OPTARG}
;;
h )
usage
;;
\? )
usage
exit 1
;;
* )
usage
exit 1
;;
esac
done

functionA

#case ${1} in
#-a)
# shift 1
# functionA ${1} ${2}
# ;;
#-b) 
# shift 1
# functionB ${1} ${2}
# ;;
#-c) 
# shift 1
# functionC ${1} ${2}
# ;;
#-d)
# shift 1
# functionD
# ;;
#-h)
# usage
# ;;
#-*)
# usage
# ;;
#*) 
# usage
# ;;
#esac
# END: Main script
# #############################################################################
Comments