wiki.getshifting.com

--- Sjoerd Hooft's InFormation Technology ---

User Tools

Site Tools


startscript

Bash: Start Script

Summary: This is a starting place for new bash scripts. It's quite old and might need some revisiting.
Date: 11 July 2011
Refactor: 3 January 2025: Checked links and formatting. Added notes.

Introduction

This is an overview of variables I use within my scripts. It makes it easier to start.

#!/bin/bash
########################################################################################################################
# Author : Sjoerd Hooft
# Date Initial Version: 11 July 2011
# Comments: sjoerd_ @ _getshifting.com
#
# Description:
# This is the description for this script.
#
# Recommendations:
# The script is designed for a 120 column terminal.
# The running user must be root.
#
# Changes:
# Please comment on your changes to the script (your name and email address, line number, description):
# DATE - USERNAME - EMAILADDRESS - CHANGE DESCRIPTION
########################################################################################################################
 
# Debug option; uncomment for debugging
# set -x
 
# Script Variables
HOSTNAME_SHORT=`hostname -s`
BASEDIR=`dirname $0`
WHATAMI=`basename $0`
LOGFILE="$BASEDIR/$WHATAMI.log"
DATE=`date +%Y%m%d`
# Highlight output on screen
BOLD=`tput bold`
BOLDOFF=`tput sgr0`
# Send all output to logfile; disable if screen output is needed
exec > $LOGFILE 2>&1
 
# Mail Variables and Function
MAILTOSUCCESS="sjoerd_warmetal.nl"
MAILTOFAILURE="alarm_warmetal.nl"
MAILTOALARM="alarm_warmetal.nl sjoerd_warmetal.nl"
MAILTOALARMCC="ccmail1_warmetal.nl ccmail2@warmetal.nl"
MAILSTATUS=success
 
mailFunction() {
   if [[ "$MAILSTATUS" -eq "success" ]]; then
      cat $LOGFILE | mail -s "Success $WHATAMI on $HOSTNAME_SHORT at $DATE" $MAILTOSUCCESS
   fi
   if [[ "$MAILSTATUS" -eq "failed" ]]; then
      cat $LOGFILE | mail -s "Failed $WHATAMI on $HOSTNAME_SHORT at $DATE" $MAILTOFAILURE
   fi
   if [[ "$MAILSTATUS" -eq "alarm" ]]; then
      cat $LOGFILE | mail -s "ALARM FOR $WHATAMI ON $HOSTNAME_SHORT AT $DATE" -c $MAILTOALARMCC $MAILTOALARM
   fi
}
 
# Start your script here

Snippet Pause the Script

# Function to pause the script
# The operator can evaluate the outcome of the previous function
scriptContinue () {
   if [ "$AUTOMATIC" == "0" ]; then
      echo "Press ENTER to continue"
      read CONTINUE
      clear
   fi
}

Snippet Script Usage

# Function to show how to use the script if the operator forgot script arguments
usageCommand() {
   echo
   echo "Usage $0 COMMAND:"
   echo "--------------------------------------"
   echo "$BOLD $0 <required arguments> $BOLDOFF"
   echo "--------------------------------------"
   echo
   echo argument: syntax
   echo
}
 
case "$1" in
 
argument )
   echo "You choose option $1"
   # Start your script here
;;
 
* )
   usageCommand
   exit 1
;;
 
esac
startscript.txt · Last modified: by 127.0.0.1