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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting While loop is not reading next line in the file when IF condition is used.
# 1  
Old 02-19-2012
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.
Code:
cat server
192.168.1.6
192.168.1.7

Code:
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
Code:
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
Code:
[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:
Code:
#!/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.

Last edited by Franklin52; 02-20-2012 at 04:42 AM.. Reason: Please use code tags for code and indent your code, thank you
# 2  
Old 02-19-2012
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.

Last edited by agama; 02-19-2012 at 11:40 PM.. Reason: clairity
This User Gave Thanks to agama For This Post:
# 3  
Old 02-19-2012
Quote:
Originally Posted by vdurai
a=$(date +"%Y-%m-%d %T")

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

count=`ssh $server tail -1 $logpath/log.0 | awk '$0>=from&&$0<=to' from="$b" to="$a" | wc -l`
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.
These 2 Users Gave Thanks to balajesuri For This Post:
# 4  
Old 02-20-2012
Quote:
Originally Posted by balajesuri
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.
You are Right!!!

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

Quote:
Originally Posted by agama
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.


Thank you so much ..Scripting is reading the lines..Thanks againSmilie

---------- 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!!!
# 5  
Old 02-20-2012
For 3rd format, you can do it this way,
Code:
$ 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.

Code:
#! /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

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

Batch file loop and increment value for condition

I am trying to have the below batch file do following two things: 1. only allow the values YES,yes,Y,y, or NO,no,N,n 2. increment the counter %var1 only if answer to question 2 is "y" and not able to get the syntax correct. If %var1%=1 then I am trying to display function :end. Thank you :).... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

Problem with while loop reading every line of a text file

Hello, I'm using RHEL 5.1 with bash. How to handle "read" inside while loop reading every line? Please see below: # cat /tmp/passwd_sample CARRJ12:qVSn4ja4mFA72,..:20021:125:JULIAN CARR:/home/everyone:/bin/bash HERCOT01:NK/3j2ZB4ZC7Q:20022:125:TOM HERCOCK:/home/everyone:/bin/bash... (4 Replies)
Discussion started by: reddyr
4 Replies

3. Shell Programming and Scripting

Reading line by line from live log file using while loop and considering only those lines start from

Hi, I want to read a live log file line by line and considering those line which start from time stamp; Below code I am using, which read line but throws an exception when comparing line that does not contain error code tail -F /logs/COMMON-ERROR.log | while read myline; do... (2 Replies)
Discussion started by: ketanraut
2 Replies

4. Shell Programming and Scripting

Reading line in while loop

Hello Team, i have to read line by line in a while loop, and the input file has:. # The script will start cppunit test application to run unit tests. -LD_LIBRARY_PATH=$CPPUNIT_HOME/lib:\ +LD_LIBRARY_PATH=$VOBTAG/SS_UnitTest/lib:\ $VOBTAG/SS_BFD/BFDSCLI/build:\ ... (7 Replies)
Discussion started by: chandana hs
7 Replies

5. Shell Programming and Scripting

Recursive search for string in file with Loop condition

Hi, Need some help... I want to execute sequence commands, like below test1.sh test2.sh ...etc test1.sh file will generate log file, we need to search for 'complete' string on test1.sh file, once that condition success and then it should go to test2.sh file, each .sh scripts will take... (5 Replies)
Discussion started by: rkrish123
5 Replies

6. Shell Programming and Scripting

Read file using while loop not reading last line

I have written a script to read the file line by line. It is reading and printing the lines. But it is coming out of loop before reading last line. So I am not able to print last line. How do I solve it. (6 Replies)
Discussion started by: dgmm
6 Replies

7. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

8. Shell Programming and Scripting

while loop not reading last line

hi all, i have a while loop which i am using to read lines into an array: k=0 exec 10<file while read LINE <&10; do ARRAY=$LINE ((k++)) done exec 10>&- echo ${ARRAY} for some reason when i display the array it is not showing the last row in the file. any help appreciated. (1 Reply)
Discussion started by: npatwardhan
1 Replies

9. Shell Programming and Scripting

Reading from a specific line in a loop

Hello All, Request you to let me know how to do the below urgently.. Requirement File A Contains: for i in file A DEV1 DEV5 STG1 STG5 File B Contains: for j in file B DEV1 DEV5 STG1 STG5 (3 Replies)
Discussion started by: kaushikraman
3 Replies
Login or Register to Ask a Question