While loop is not reading next line in the file when IF condition is used.

Hi Guys

I am new to scripting.Please forgive for asking basic questions.

I want to write a script to check whether the logs are getting updated in last 15 mins.

cat server
192.168.1.6
192.168.1.7
cat list
192.168.1.7 /logs/logpath1
192.168.1.7 /logs/logpath2
192.168.1.6 /logs/logpath3
192.168.1.6 /logs/logpath4

Sample log.0 file ##Present on above mentioned server and path

2012-02-18 22:17:06.768 | STATUS:SUCCESS 
2012-02-18 22:18:06.768 | STATUS:SUCCESS
2012-02-18 22:19:06.768 | STATUS:SUCCESS
2012-02-18 22:20:06.768 | STATUS:SUCCESS 

Issue :

While is not reading the next line in the /try/logpath

[root@venkat try]# grep 192.168.1.6 /try/list | awk {'print $2'} > /try/logpath
[root@venkat try]# 
[root@venkat try]# cat logpath 
/logs/logpath1
/logs/logpath2

Its reading only the first line in the logpath file and when IF condition is executed, its comes out of the loop.Its not reading the next line in logpath file.I want it to read the next line as well and check in that path also.

Please tell me the error in this script.

Looking forward to your suggestions.

======================================================
Script:

#!/bin/bash
> /try/updating
> /try/notupdating

###Script to find out whether logs are getting updated in last 15 min.

a=$(date +"%Y-%m-%d %T")

echo "Current Time :$a"

b=$(date +"%Y-%m-%d %T" --date="15 mins ago")

echo "Time before 15 Mins :$b"

while read server
do

echo "=> processing logs on ${server}"

echo "=> path to be checked"

grep $server /try/list | awk {'print $2'} > /try/logpath 

while read logpath
do

count=`ssh $server tail -1 $logpath/log.0 | awk '$0>=from&&$0<=to' from="$b" to="$a" | wc -l`

if [ $count -gt 0 ]
then

echo "$server $logpath Logs are updating" >>/try/updating

else

echo "$server $logpath Please Check Logs are not updating" >>/try/notupdating

fi

done < /try/logpath

done < /try/server

========================================================

Thank you in Advance.

A while loop with redirection or piped input reads from stdin. Ssh by default also reads stdin and ends up reading the remainder of the data intended for the while.

Add -n to your ssh command to prevent it from reading from stdin.

1 Like

The awk statement doesn't seem to be working the way you intend it to.

  1. Keep date in this format for easier numeric comparison: yyyymmddhhmiss (20120220034005). Two such numbers are easier for comparison.

  2. If date is in this format: "2012-02-20 03:40:05", how can you compare it with some other date in the same format using > and < symbols?

  3. When you say $0 in awk, it refers to the whole line. I see that log.0 file contains lines in this format: "2012-02-18 22:18:06.768 | STATUS:SUCCESS", so you're trying to compare this whole line with $a and $b.

  4. You can reduce this line to "2012-02-18 22:18:06" by defining the field separator as "." and taking $1 into account. And further reduce it to "20120218221806" by removing hyphen and colon.

2 Likes

You are Right!!!

---------- Post updated at 11:15 PM ---------- Previous update was at 11:14 PM ----------

Thank you so much ..Scripting is reading the lines..Thanks again:)

---------- Post updated at 11:39 PM ---------- Previous update was at 11:15 PM ----------

My log file will have only this format 2012-02-18 22:18:06.768...Is it a way that we can compare any two dates...irrespective of the date format in the log file.

Ex format :

1.Feb 19 19:33:09
2.19/FEB/2012 13:55:36
3.2012-02-18 22:18:06.768

Thank You!!!

For 3rd format, you can do it this way,

$ a='2012-02-18 22:18:06'
$ b=$(date -d "$a" +%Y%m%d%H%M%S)
$ echo $b
20120218221806

For format 1 and 2, you have to do further processing. Here an example to parse format #2.

#! /bin/bash
a='19/FEB/2012 13:55:36'
x=( JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC )
for mon in `seq 0 11`
do
    if [ "${a:3:3}" == "${x[mon]}" ]
    then
        mon=$(($mon + 1))
        break
    fi
done
b=${a:7:4}$(printf "%02d" $mon)${a:0:2}${a:12:2}${a:15:2}${a:18:2}
echo $b