Register
It is currently Tue Jul 22, 2014 3:16 am

loop testing


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Tue Sep 10, 2013 3:34 pm   

Joined: Tue Sep 10, 2013 3:07 pm
Posts: 4
My first post

I have to test for a number of text strings in a logfile
Code:
strings=`"error sting one" "error string two" "error string three"`
for p in $strings ;   do
     count=` tail /my/log/file -n 20 | grep -ic $p `
     echo "testing for $p and found it $count times" >> testlog.file
done


the problem with the above is that it loops through each word of the 3 text strings and not the full string.

Hope some can help

Cheers


Top
 Profile  
 PostPosted: Tue Sep 10, 2013 5:45 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 532
hello,

as ` ` is to substitute commands' output, and there is no command, the loop should never happen.

Use an array.

however, grep can test the whole of the three eror messages at once; whithout a loop.
Code:
grep 'error \(one\|two\|three\)' <(tail file)


Top
 Profile  
 PostPosted: Wed Sep 11, 2013 3:12 am   

Joined: Tue Sep 10, 2013 3:07 pm
Posts: 4
thanks for the reply.

What I basically need is to count how many times each error string occurs in the last 20 lines of my log file and based then on the size of this number execute different functions.

This could be done by creating a grep statement and variable for every error string, but there is a chance that more error string may be added using a loop would simplify the script

your proposal will accumulate the number of times the errors occur?

cheers

I could also have written the code better

Code:
error1="it overloaded"
error2="the service died"
error3="It lost to the overlord"

strings=" $error1 $error2 $error3"

for p in $strings ;  do
     count=` tail /my/log/file -n 20 | grep -ic $p `
     echo "testing for $p and found it $count times" >> testlog.file
done


Top
 Profile  
 PostPosted: Wed Sep 11, 2013 6:12 am   

Joined: Tue Sep 10, 2013 3:07 pm
Posts: 4
arh... found the solution using IFS (Internal Field Separator) like this will make sure the spaces in the string arguments are ignored or rather I will use "," to split the string instead.

Code:
error1="it overloaded"
error2="the service died"
error3="It lost to the overlord"

strings=" $error1,$error2,$error3"

IFS=$','
for p in $strings ;  do
     count=` tail /my/log/file -n 20 | grep -ic $p `
     echo "testing for $p and found it $count times" >> testlog.file
done
unset IFS



Top
 Profile  
 PostPosted: Wed Sep 11, 2013 1:13 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 532
use an array:
Code:
#!/bin/bash

errors=(
   "it overloaded"
   "the service died"
   "It lost to the overlord"
)
lastLines=$(tail -20 /to/log/file)

for e in "${errors[@]}"
do
   if test $(grep -ic "$e" <<<"$lastLines") -ge $threshold
   then
      : do something
#   else
#      : do something else
   fi
done


Top
 Profile  
 PostPosted: Fri Sep 13, 2013 3:50 am   

Joined: Tue Sep 10, 2013 3:07 pm
Posts: 4
nice - thanks


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP