Hey guys - I have made this script and for some reason, I dont see time stamp as "Month-Day-YYYY Hours-Mins" - all i see is Month and Day.
Also, my file size is approximated. For example, if the size is 19,606KB - the script shows as 20M. Is there a way to see the exact file size?
How do i accomplish the above tasks?
#!/bin/bash
# setting default path
PATH="/usr/bin:/bin:/usr/sbin"
NOW=$(date +"%Y-%m-%d")
LOG_FILE=/tmp/myemail.txt
FILE_NOT_FOUND="/tmp/missingfiles.txt"
CURRENT_FILE="/tmp/currentfiles.txt"
OLD_FILE="/tmp/oldfiles.txt"
# make sure we create a new copy of these files
rm -f $LOG_FILE $FILE_NOT_FOUND $CURRENT_FILE $OLD_FILE
# List the directories and files to process.
DIR="dir1 dir2"
FILE="file 1 file 2"
#############################
# Define functions
#############################
function checkStat
{
# make sure stat command is installed
which stat > /dev/null 2>&1
if [ $? -eq 1 ]; then
echo "stat command not found!"
exit 2
fi
}
function processFile
{
if [ -s ${1} ]; then
VAR="$(stat -c %y ${1})"
VAR2="${VAR:0:10}"
FILE_TIME=$(ls -l $1| cut -d" " -f6,7)
FILE_SIZE=$(du -sh $1 | cut -f1)
if [ $NOW == $VAR2 ]; then
echo "Current File Found: ${1} $FILE_TIME - SIZE $FILE_SIZE" | tee -a ${CURRENT_FILE}
else
erroremail=1
echo "Old File Found: ${1} $FILE_TIME SIZE - $FILE_SIZE" | tee -a ${OLD_FILE}
fi
else
erroremail=1
echo "File Not Found: ${1}" | tee -a ${FILE_NOT_FOUND}
fi
}
#############################
# Main
#############################
# Verify the stat command exists
checkStat
# Create log file
touch ${LOG_FILE}
# Process the files
for d in ${DIR[@]}; do
for f in ${FILE[@]}; do
processFile "${d}/${f}"
done
done
#############
#Email
#############
if [ $erroremail -eq 1 ] ; then
SUBJECT="ERROR: File Problem"
else
SUBJECT="OK: File Current"
fi
# lets populate the sendmail files
echo "To:someone@nowhere.com" >>$LOG_FILE
echo "Subject:$SUBJECT" >> $LOG_FILE
# Append list of current files to Email body
# Make sure we display error message only once
error_display=false
# Append list of old files to Email body
if [ -s $OLD_FILE ]; then
echo "*********************ERROR MESSAGES********************" >> $LOG_FILE
error_display=true
echo "List of old files" >> $LOG_FILE
cat $OLD_FILE >> $LOG_FILE
fi
# blank lines between two data
echo >> $LOG_FILE
echo >> $LOG_FILE
# Append list of missing files to Email body
if [ -s $FILE_NOT_FOUND ]; then
if [ "$error_display" = "false" ]; then
echo "*********************ERROR MESSAGES********************" >> $LOG_FILE
fi
echo "List of missing files" >> $LOG_FILE
cat $FILE_NOT_FOUND >> $LOG_FILE
fi
# blank lines between two data
echo >> $LOG_FILE
echo >> $LOG_FILE
if [ -s $CURRENT_FILE ]; then
echo "****************GOOD MESSAGES*************************" >> $LOG_FILE
echo "List of current files" >> $LOG_FILE
cat $CURRENT_FILE >> $LOG_FILE
fi
# send an email using /sbin/sendmail
sendmail -t < $LOG_FILE
if [ $? -eq 0 ]; then
echo "Sent mail successfully"
else
echo "Error sending mail"
fi
# cleanup files
rm -f $LOG_FILE $FILE_NOT_FOUND $CURRENT_FILE $OLD_FILE