BASH脚本检查当前和上个月的日志文件

时间:2015-05-29 14:26:34

标签: bash date for-loop

我在过去的两个月里一直在研究这个问题,尽管我看了很多次,但我无法让它发挥作用。 此脚本会检查每日日志文件中是否有用户定义的变量,因此他们不必手动查看每个日志文件。它检查当前月份非常好,但是如果用户想要检查20天,今天是本月12日,那么我希望能够回到上个月(不要查找带有日期的日志文件)日期20150399等)。我已经检查了我的日期/日计算的逻辑,它们似乎没问题(如果在BASH中有更好的方法,我愿意接受建议)。我尝试调试时会发生意外的文件结束。我对编写包含超过20行左右的脚本有些新意,但我无法想出我所遗漏的内容。 我尝试了各种修复,无济于事,但我认为这是最后一次迭代。 想法?

#!/bin/bash
########################################################
# multi_log_chk.sh
# This script will take input from the user and report which
# CyberFusion MFT logs contain what the user is looking for.
# Hopefully this will save the user having to search through every
# stinking log file to find what they are looking for.
# 20150406 pxg007 started typing
# 20150413 pxg007 added && comparison for back out (line 28)
#                 added message for no entries found (line 32, 38, 48-52)
#                                 Added some further description (line 16)
# 20150424 pxg007 Added logic to calculate previous month and if necessary, year. (Lines 16-24, 60-78 )
#
########################################################

currDate=`date +%d%B%C%y`
currDay=`date +%d`
currMnth=`date +%m`
currYear=`date +%C%y`
 case $currMnth in                                                                                              #Let's establish number of days for previous month
        05 | 07 | 10 | 12 )  lastMnthD=30;;
        01 |02 | 04 | 06 | 09 | 08 | 11 )  lastMnthD=31;;
        03 ) lastMnthD=28;;                                                                             ##and screw leap year
esac
if [ $currMnth -eq 01 ]; then                                                                   ##accounting for January
        lastMnth=12
        else
        lastMnth=$((currMnth-1))
fi
if [ $lastMnth -eq 12 ]; then                                                           ## accounting for Dec of previous year
        lastMnthYr=$((currYear-1))
        else
        lastMnthYr=$currYear
fi


echo "This script will find entries for your query in whatever available MFT logs you request."
echo " "
echo "For instance - how many log files have transfer entries with \"DOG\" in them?"
echo " "
echo "I also will also give an estimate of how many transfers per log file contain your query, give or take a couple."
echo " "
echo "This search is case sensitive, so \"DOG\" is *** NOT *** the same as \"dog\""
echo " "
read -p "What text you are looking for? Punctuation is okay, but no spaces please. " looking          ### what we want to find
echo " "
echo "Today's date is: $currDate."
echo " "
read -p "How many days back do you want to search(up to 25)? " daysBack                 ### How far back we are going to look
        if [ "$daysBack" == 0 ]  && [ "$daysBack" >> 25 ]; then
        echo "I said up to 25 days. We ain't got more than that!"
         exit 1
        fi
echo " "
echo "I am going to search through the last $daysBack days of log files for:\"$looking\" "
echo " "
read -p "Does this look right? Press N to quit, or any other key to continue:  " affirm
        if [ "$affirm" = N ] && [ "$affirm" = n ]; then  ###Yes, anything other than "N" or "n" is a go
         echo "Quitter!"
         exit 1
           else
                nada=0                                          ### Used to test for finding anything
                backDate=$((currDay-daysBack))                  ### current month iterator (assuming query covers only current month)
                if (("$daysBack" => "$currDay")); then                  ## If there are more logs requested than days in the month...
                        lastMnthCnt=$((daysBack-currDay))                               ### how many days to check last month
                        lastMnthStrt=$((lastMnthD-lastMnthCnt))                 ## last month start and iterator
                        backDate=$(currDay-(daysBack-lastMnthCnt))              # Setting the iterator if we have to go back a month

                                while (("$lastMnthStrt" <= "$lastMnthD" )); do
                                foundIt=$(grep "$looking" /CyberFusion/log/Log.txt."$lastMnthYr$lastMnth$lastMnthStrt" | parsecflog | wc -l )
                                howMany=$((foundIt/40+1))                       ### Add one in case there are less than 40 lines in the record.
                                        if (("$foundIt" > 0))
                                        then
                                        nada=$((nada+1))
                                        echo "Log.txt.$lastMnthYr$lastMnth$lastMnthStrt contains $looking in approximately $howMany transfer records."
                                        lastMnthStrt=$((lastMnthStrt+1))
                                        echo " "
                                        else
                                        lastMnthStrt=$((lastMnthStrt+1))
                                        fi
                fi
                        backDate=$((currDay-daysBack))                  ### current month iterator (assuming query covers only current month)
                                while (("$backDate" <= "$currDay")); do
                                foundIt=$(grep "$looking" /CyberFusion/log/Log.txt."$backDate" | parsecflog | wc -l )
                                howMany=$((foundIt/40+1))                                               ### Add one in case there are less than 40 lines in the record.
                                        if (("$foundIt" > 0))
                                        then
                                        nada=$((nada+1))
                                        echo "Log.txt.$backDate contains $looking in approximately $howMany transfer records."
                                        backDate=$((backDate+1))
                                        echo " "
                                        else
                                        backDate=$((backDate+1))
                                        fi


                        if [ "$nada" \< 1 ]
                         then
                         echo " "
                        echo "I found no entries for $looking in any log file."
                        fi

1 个答案:

答案 0 :(得分:0)

你在第81和96行错过了关键字'done',在最后一行也错过了最后的'fi'关键字。

还有其他人建议你可以做

date -d "20 days ago" +"%d%B%C%y"

轻松获取过去的日期