Hi guys - below is my script that is checking for current file, size and timestamp.
However I added a "grep" feature in it (line in red), but not getting the desired result.
I am trying to acheive in output:
- Show me the file name, timestamp, size and grep'ed words
It would be a good message, if all of the words are found and file is from todays date and has a size above 0KB.
It would be a error message, if either both words are not found OR even one of them is not found, file is not found, file is old or is 0KB.
#!/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/system2/rie-share"
FILE="IUYhkTC`date '+%m%d%Y'`*.txt"
#############################
# 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 ] && grep -E -i "login|Successfully" ${1} > /dev/null; 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@somewhere.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
This is the output error i get:
./orcheck.sh: line 52: [: "/dir1/system2/rie-share/IUYhkTC`date '+%m%d%Y'`*.txt: binary operator expected
File Not Found: "/dir1/system2/rie-share/IUYhkTC`date '+%m%d%Y'`*.txt
Sent mail successfully
Notice my file name that is being searched. It has the date in its name and a randomly changing number followed by the date.
Thanks