2015-05-29 77 views
2

過去兩個月我一直在做這個開關,儘管我看了很多次,但無法完成工作。 此腳本檢查用戶定義變量的每日日誌文件,因此他們不必手動查看每個日誌文件。它很好地檢查了當前月份,但如果用戶想要檢查20天,並且今天是本月12日,我希望能夠返回到前一個月(不查找帶有日期20150399等)。我已經檢查了我的日期/日計算的邏輯,並且它們看起來沒問題(如果在BASH中有更好的方法可以做到這一點,我願意接受建議)。當我嘗試調試時會發生什麼意外的文件結束。對於編寫包含20多行代碼的腳本,我有點新,但我不能想出我缺少的東西。 我試過各種修補程序,但無濟於事,但我認爲這是最後一次迭代。 想法?BASH腳本檢查當前月份和上個月的日誌文件

#!/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 
+4

日期你有沒有試着去'日期--date = 「 - 1天」 +「%d%B% C%Y「'? – leu

+0

在這個問題的第一個'echo'行是否與這個問題有關?因爲如果不是,你應該從帖子中刪除它。 –

+0

我認爲這是。我很確定問題出現在if或while語句中的某處。我粘貼了整個事情,只是爲了更好地瞭解我是如何處理整個混亂的。 –

回答

0

您在第81行和第96行缺少關鍵字'done',並且在最後一行還有一個最終'fi'關鍵字。

也爲其他人則建議你可以做

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

輕鬆獲得過去

相關問題