SH/BASH - Scan a log file until some text occurs, then exit. How??

Posted by James on Stack Overflow See other posts from Stack Overflow or by James
Published on 2010-04-06T19:49:48Z Indexed on 2010/04/06 19:53 UTC
Read the original article Hit count: 309

Filed under:
|
|
|

Current working environment is OSX 10.4.11.

My current script:

#!/bin/sh
  tail -f log.txt | while read line 
do
  if echo $line | grep -q 'LOL CANDY'; then
    echo 'LOL MATCH FOUND'
    exit 0
  fi
done

It works properly the first time, but on the second run and beyond it takes 2 occurrences of 'LOL CANDY' to appear before the script will exit, for whatever reason. And although I'm not sure it is specifically related, there is the problem of the "tail -f" staying open forever. Can someone please give me an example that will work without using tail -f?

If you want you can give me a bash script, as OSX can handle sh, bash, and some other shells I think.

© Stack Overflow or respective owner

Related posts about log

Related posts about scan