监视器文件
问题描述:
场景bash脚本是 -监视器文件
在位置的/ opt /数据/有10差分文件与名与前一天结束
如 - file_1_20160627
我需要检查这10个文件是否存在。
如果存在,我需要显示输出 - “OK - file_1_20160627存在”和写入输出/tmp/report.txt文件
如果文件不存在,我想的一样如上 - “失败 - file_1_20160627不存在”和写入输出在同/tmp/report.txt文件脚本运行时每天
,内容在该文件上必须更换。
我试图写,但我不擅长脚本。下面的脚本仅适用于4个文件。我觉得很多事情都需要改变。
感谢有人帮我创建这个脚本。
#!/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
答
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
什么* *明确你需要哪些帮助?我们不会为你写整个脚本。你坚持什么特定的部分? –
实际循环部分有点困惑 – lfreez