views:

219

answers:

2

I am writing a shell script to do a "tail" on a growing log file. This script accepts parameters to search for, and incrementally greps the output on them.

For example, if the script is invoked as follows:

logs.sh string1 string2

it should translate to:

tail -f logs.txt | grep string1 | grep string2

I am building the list of grep strings like this:

full_grep_string=""
for grep_string in $*
do
    full_grep_string="$full_grep_string | grep $grep_string" 
done

The string is built correctly, but when I try to finally tag it to the tail command, like so...

tail -f $LOG_FILE_PATH $full_grep_string

...the grep does not apply, and I get the unfiltered logs.

Am I missing something here? Or is there an easier way to do this?

+2  A: 
eval tail -f $LOG_FILE_PATH $full_grep_string
an0
It worked! Thanks! :)
Phanindra K
+1  A: 

grep buffers the line it found. So modifying your code to

full_grep_string="$full_grep_string | grep --line-buffered $grep_string"

shall work. I tested it on debian lenny (with bash).

And use the tip of an0

eval tail -f ...

(All this works for whole words)

tuergeist
Thanks for the tip.. but it looks like my version of grep is too old to take full word arguments. I'm testing on a SunOS 5.8 box, btw. Very old software. :)
Phanindra K