lfreez lfreez - 5 months ago 31
Bash Question

Bash script for monitor files

scenario is -

In the Location /opt/data/ there are 10 difference files with name ending with previous day

eg - file_1_20160627

I need to check whether these 10 files exist or not.

If one exists, I need to show output - "OK - file_1_20160627 exist" and write output in /tmp/report.txt file

If a file does not exist, I want the same as above - "Failed - file_1_20160627 not exist" and write output in the same /tmp/report.txt file

Every day when the script runs, content on that file must replace.

I tried to write but I'm not good in scripting. below script only for 4 files. I think so many things need to be change.

appreciate someone help me to create this script.

#!/bin/bash

now=`date +%Y%m%d%H%M%S`
time=`date +%H%M`
week=`date +%a`


/bin/rm -f /tmp/report.txt

if [ "$time" -ge 1300 ] && [ $time -lt 2359 ]; then
if [ "$week" == Sun ]; then

if [ -f "find /opt/data/ -type f -name "file_1_`date -d "1 day ago" +%Y%m%d`.txt"" ];
then
echo "OK - file_1 file does exist" >> /tmp/report.txt
else
echo "Failed - file_1 file does not exist." >> /tmp/report.txt

fi

if [ -f "find /opt/data/ -type f -name "file_2_`date -d "1 day ago" +%Y%m%d`.txt"" ];
then
echo "OK - file_2 file exist." >> /tmp/report.txt
else
echo "Failed - file_2 file does not exist" >> /tmp/report.txt
fi
else

fi

if [ -f "find /opt/data/ -type f -name "file_3_`date -d "1 day ago" +%Y%m%d`.txt"" ];
then
echo "OK - file_3 file exist" >> /tmp/report.txt
else
echo "Failed - file_3 file does not exist" >> /tmp/report.txt
fi

if [ -f "find /opt/data/ -type f -name "file_4_`date -d "1 day ago" +%Y%m%d`.txt"" ];
then
echo "OK - file_4 file exist" >> /tmp/report.txt
else
echo "Failed - file_4 file does not exist" >> /tmp/report.txt
fi
else

fi

Answer
PREFIX="/opt/data"
REPORT="/tmp/report.txt"
DATE=$( date +%Y%m%d )

rm "$REPORT"

for i in `seq 1 10`;
    do

            FILENAME="file_${i}_${DATE}"
            FULLFN="$PREFIX/$FILENAME"
            if [ -f "$FULLFN" ]; then
                    echo "OK - $FILENAME exists" >> $REPORT
            else
                    echo "Failed - $FILENAME do not exist" >> $REPORT
            fi
    done
Comments