Generic script framework 1.

This script framework is intended for scripts which are run via cron for example, it doesn't try to handle command line arguments as might be needed in an interactive script.

#!/bin/bash

# Written Mr User <DATE>

# Purpose of Script
# =================
#
#                                                                                                                                    
#                                            
                                                                                                                                      
# turn on debugging, -x starts, +x stops                                                                                               
set +x                                                                                                                                

# Variables
# =========
                                                                                                                                      
# set to 3 for testing, this forces sending mail, normally should be 0                                                                 
SENDEMAIL=0                                                                                                                           
                        
EMAIL=user@mycompany.com
LOGFILE=/path/to/logfile/log.txt
TMPLOG=/tmp/tmplog.txt-$$

DATE=`date +%b" "%d" "%Y`
DAY=`date +%a`
DATENUM=`date +%d`
echo " " >> ${LOGFILE}
echo -n "${DATE} - " >> ${LOGFILE}


# Test to force 1st of the month
# DATENUM=01
if [ $DATENUM == 01 ]
then
        MONTHTEST=" - 1st of the month test email."
        SENDEMAIL=2
fi

# Start of code
# =============

echo "<What I'm going to do text>" > ${TMPLOG}
echo "================================" >> ${TMPLOG}
echo "" >> ${TMPLOG}
echo "From /path/to/sctipt/scriptname.sh on `hostname`" >> ${TMPLOG}
echo "" >> ${TMPLOG};
echo "" >> ${TMPLOG};

<code>
.
.
.
.
</code>

  #If any errors encountered, send email.
if [ ${CPRESULT} != 0 ] || [ ${CPUSERRESULT} != 0 ]
        then
        SENDEMAIL=1
fi


# Code Finishes here
# ==================

# Send email
# ==========

if [ ${SENDEMAIL} -eq 1 ]
        then
        /bin/mailx -s "Failure reason `hostname`." ${EMAIL} < ${TMPLOG}
elif [ ${SENDEMAIL} -eq 2 ]
        then
        /bin/mailx -s "Failure reason `hostname`, ${MONTHTEST}." ${EMAIL} < ${TMPLOG}
elif [ ${SENDEMAIL} -eq 3 ]
        then
        /bin/mailx -s "scriptname.sh on `hostname`, set to testing." ${EMAIL} < ${TMPLOG}
else
        echo "No errors" > /dev/null
fi


# Clean up any mess and finish
# ============================
rm ${TMPLOG}

# exit with return status reflecting how it all went.
exit ${SENDEMAIL}
 
linux/bash-generic-script-framework1.txt · Last modified: 10/11/2022 10:28 by andrew