【问题标题】:BASH script checking log files for current and previous monthBASH 脚本检查当前和上个月的日志文件
【发布时间】:2015-08-12 11:19:48
【问题描述】:

在过去的两个月里,我一直在断断续续地处理这个问题,尽管我看了很多次,但我无法让它发挥作用。 此脚本检查用户定义变量的每日日志文件,因此他们不必手动查看每一个。检查当前月份效果很好,但是如果用户想检查 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

【问题讨论】:

  • 你试过date --date="-20 days" +"%d%B%C%y"吗?
  • 第一行echo 下的内容与这里的问题有关吗?因为如果不是,您应该将其从帖子中删除。
  • 我认为是。我很确定问题出在 if 或 while 语句之一中。我粘贴了整个内容,只是为了更好地了解我对整个混乱的处理方式。
  • 将您的脚本粘贴到shellcheck.net。有几个语法错误。
  • 谢谢 rojomke!我知道一定有什么地方可以比 BASH -x 更好地评估我的代码。这正是我所需要的。

标签: bash date for-loop


【解决方案1】:

您在第 81 行和第 96 行缺少关键字“done”以及最后一行的最终关键字“fi”。

正如其他人建议的那样,你可以这样做

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

轻松获取过去的日期

【讨论】:

    猜你喜欢
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-23
    • 1970-01-01
    相关资源
    最近更新 更多