0

Solution: My date variables were in the wrong format (day number and day of the week were flipped). I changed this, then used the if statement proposed by @PesaThe below instead of my test.

Original Post:

I am writing a bash script to run as part of my servers' daily maintenance tasking. This particular job is to search for entries in input_file matching yesterday's and today's time stamps. Here are my date variables.

today=$(date "+%a, %b %d %Y")
yesterday=$(date --date=yesterday "+%a, %b %d %Y")

Here are the declarations, which are exactly as they should be:

declare -- adminLogLoc="/opt/sc/admin/logs/"
declare -- adminLog="/opt/sc/admin/logs/201801.log"
declare -- today="Tue, Jan 02 2018"
declare -- yesterday="Mon, Jan 01 2018"
declare -- report="/maintenance/daily/2018-01-02_2.2.txt"

Here are some actual log entries like those I need output. These were found with grep $today $adminLog | grep error

Tue, 02 Jan 2018 14:38:50 +0000||error|WARNING|13|Query #2464 used to generate source data is inactive. Tue, 02 Jan 2018 14:38:50 +0000||error|WARNING|13|Query #2468 used to generate source data is inactive. Tue, 02 Jan 2018 14:38:50 +0000||error|WARNING|13|Query #2470 used to generate source data is inactive. Tue, 02 Jan 2018 14:38:50 +0000||error|WARNING|13|Query #2474 used to generate source data is inactive.

Here is the if statement I am trying to run:

# Check for errors yesterday
if [ $(grep $yesterday $adminLog|grep "error") != "" ]; then
    echo "No errors were found for $yesterday." >> $report
else
    $(grep $yesterday $adminLog|grep "error") >> $report
fi
# Check for errors today (at the time the report is made, there
# probably won't be many, if any at all)
if [ $(grep $today $adminLog|grep "error") != "" ]; then
    echo "No errors were found for $today." >> $report
else
    $(grep $today $adminLog|grep "error") >> $report
fi

I have tried this several ways, such as putting double quotes around the variables in the test, so on. When I run the grep search in the command line after setting the variables, it works perfectly, but when I run it in the test brackets, grep uses each term (i.e. Tue, Jan... so on) as individual arguments. I have also tried

grep $yesterday $adminLog 2> /dev/null | grep -q error
if [ $? = "0" ] ; then

with no luck. How can I get this test to work so I can input the specified entry into my log file? Thank you.

jww
  • 97,681
  • 90
  • 411
  • 885
Matt
  • 9
  • 1
  • 4
  • 2
    grep "$yesterday" "$adminLog" – EdmCoff Jan 02 '18 at 14:10
  • [Security implications of forgetting to quote a variable in bash/POSIX shells](https://unix.stackexchange.com/q/171346/4667) – glenn jackman Jan 02 '18 at 14:15
  • Also, maybe try http://shellcheck.net/ before asking for human guidance. – tripleee Jan 02 '18 at 14:17
  • What about: `if ! output=$(grep "$yesterday" "$adminLog" | grep "error"); then echo "nothing" >> "$report"; else echo "$output" >> "$report"; fi` – PesaThe Jan 02 '18 at 14:17
  • This is not a duplicate question. I have already read the question you said is exactly the same before I asked. The problem is not with the expansion, the problem is that once it's put into a test, for some reason, nothing I do makes it search properly. – Matt Jan 02 '18 at 14:22
  • @PesaThe that works only if I remove the quotation marks – Matt Jan 02 '18 at 15:09
  • Without quotes, the expansions will undergo word-splitting. That's not what you want. I think either `$adminLog` or `$report` contain something different than you think. Try to `declare -p adminLog` to see what's the real content of your variables. – PesaThe Jan 02 '18 at 15:12
  • @PesaThe I ran that for `adminLog`, `yesterday`, and `report`, and they all gave me exactly what I was expecting. The only thing I can think of is maybe the problem arises from `$today` and `$yesterday` having spaces, but they need to because that's the string I need to search. – Matt Jan 02 '18 at 15:18
  • @Matt you may want to include those in the question. – PesaThe Jan 02 '18 at 15:22
  • @PesaThe done, thank you - I'm still a noob to using SO – Matt Jan 02 '18 at 15:27
  • @Matt now you can delete the `echo` commands and also include at least a part of the log. – PesaThe Jan 02 '18 at 15:30
  • @PesaThe done . . – Matt Jan 02 '18 at 15:44
  • @Matt Agh, the format of your date is incorrect...`Tue, 02 Jan 2018` vs `Tue, Jan 02 2018`. Not quoting the variables just seemingly worked but did something completely different. That's why you should always include input. See: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – PesaThe Jan 02 '18 at 15:48
  • @PesaThe what a rookie mistake! That changes everything. I tried your `if` statement and it works properly now. Thank you. I will change my scripts to run that. – Matt Jan 02 '18 at 16:08

1 Answers1

0

Could you please try following script and let me know if this helps you. This snippet will help to simply print the yesterday's and today's logs in case you want to take them into a output file or so, we could adjust it accordingly too then.

#!/bin/bash
today=$(date "+%a, %b %d %Y")
yesterday=$(date --date=yesterday "+%a, %b %d %Y")    

todays=$(grep "$today" Input_file)
yesterdays=$(grep "$yesterday" Input_file)

if [[ -n $todays ]]
then
        echo "$todays"
else
        echo "no logs found for todays date."
fi

if [[ -n $yesterdays ]]
then
        echo "$yesterdays"
else
        echo "NO logs found for yesterday's date."
fi
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • Hi @RavinderSingh13, I ran it and got the else statement returned, even though there are entries in there for both days. – Matt Jan 02 '18 at 14:29
  • @Matt, you need to make sure that date format what we are mentioning into script is same as in logs else it will always say is haven't found anything. – RavinderSingh13 Jan 02 '18 at 14:30
  • I got my files mixed up, it's another log file I'm trying to search, not /var/log/messages. Let's try it again... – Matt Jan 02 '18 at 14:35
  • @Matt, sure give it a shot and let us know how it goes then. – RavinderSingh13 Jan 02 '18 at 14:36
  • I ran what you said (above) in just a test script. Both times, I got the else statement returned. So I ran them all separately in the shell. I set all variables and tried to echo $todays and got nothing returned. So I ran just `grep "$today" Input_file` and got no result. But when I run `grep $today Input_file` I get the results I'm looking for. So, I tried setting `todays=$(grep $today Input_file)` and that's when I get `grep: Jan: No such file or directory grep: 02: No such file or directory grep: 2018: No such file or directory` – Matt Jan 02 '18 at 15:11
  • @Matt, did you check the date format in logs? Also `echo "$todays"` should give you output. – RavinderSingh13 Jan 02 '18 at 15:17
  • `echo $todays` does give me the output, but I need it filtered to only the entries containing the string `error` and can't seem to figure out a way to do that. – Matt Jan 02 '18 at 15:35
  • @Matt, change `todays=$(grep "$today" Input_file | grep "error")` and `yesterdays=$(grep "$yesterday" Input_file | grep "error")` in above code and let me know if this helps. NOTE: Error could in either `error` or `Error` or `ERROR` form so please grep it accordingly too. – RavinderSingh13 Jan 02 '18 at 15:39
  • I tried that, but `echo $todays` returns nothing. Also, `error` is as it should be, it's only a lowercase string in the logs. – Matt Jan 02 '18 at 15:43