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:
1. 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.
Code:
#!/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:
Code:
./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.
HI Guys hoping some one can help
I have two files on both containing uk phone numbers
master is a file which has been collated over a few years ad currently contains around 4 million numbers
new is a file which also contains 4 million number i need to split new nto two separate files... (4 Replies)
hi guys
very new to this game so excuse my ignorance. I need to create a script that simply greps for a text string and then outputs a message depending on whether the text string is there or not. The script I have setup is below, but whenever I run it I get the following error:
... (5 Replies)
I need help in the following script. I want to grep the sql errors insert into the error table and exit the shell script if there is any error, otherwise keep running the scripts.
Here is my script
#!/bin/csh -f
source .orapass
set user = $USER
set pass = $PASS
cd /opt/data/scripts
echo... (2 Replies)
Hi guys, I have written this script, however the outcome is invalid. It contains grep search that is not needed:
Script:
#!/bin/bash
#this is a test script
FILES=$(ls /home/student/bin/dir1/*)
GREPFUNC=$(grep -E -i "login|Successfully" ORProxyTC`date '+%m%d%Y'`*.txt/ ${FILES})... (14 Replies)
Hi,
I have written the following shell script -
Error_String="error"
var1="| grep -v note1 | grep -v note2"
grep -i $Error_String /users/mqm/Pwork/Err/*.out $var1
The above script gives error saying "grep: can't open |
grep: can't open grep
grep: can't open -v" etc
In my program... (3 Replies)
Hi,
I am writing a shell script that checks catalina logs on a production system and mails me if it detects errors.
It greps the logs for known errors which i have defined as variables.
The problem is the logs are huge, approx 30,000 before they rotate.
So I am forced to use grep instead... (3 Replies)
Hello
I do want to write a script which will check any errors say "-error" in the log file then have to send email to the concern person . And the concern person will correct the error .
Next time if the script runs eventhough the error has been corrected it will ... (1 Reply)
Hi,
I have a problem with a simple script I am trying to write. I want a user to type grep, sed commands that are then stored in variables. Those variables are stored in a function, and the function is then called to execute the commands. The idea is that the user does it step by step.
... (4 Replies)
Hi everyone,
Im trying to write a Shell script that basically creates a set of files based on a file with many records. For example if a file called dummy has the following content:
a.txt
1st line of a's text file
2nd line of a's text file
3rd line of a's text file
b.txt
1st line of b's... (8 Replies)