You don't know how to use the three bash shells?
July 9, 2013 10:47 PM   Subscribe

There is this bash script that should take the contents of a log file, grep for a keyword, and then send the entire line to another script. It isn't doing that, and I'm not sure why. Can you spot the error?

Two variations, neither really works:

#!/bin/bash
tail -Fn0 /path/to/log/file.log | \
while read line ; do
SOMETHING=$(echo "$line" | grep "Keyword")
if [ $? = 0 ]
then
/path/to/other/script.sh "Unimportant string" 1 "${line}"
fi
done

#!/bin/bash
tail -Fn0 /path/to/log/file.log | grep "Keyword" | xargs -n 1 /path/to/other/script.sh "Unimportant string" 1
posted by TimeDoctor to Computers & Internet (10 answers total) 1 user marked this as a favorite
 
Does the first part work, before the pipe?
posted by oceanjesse at 10:58 PM on July 9, 2013


It looks like xargs doesn't do anything until the pipe is closed (which it never will be with the tail -F)
posted by spbmp at 11:02 PM on July 9, 2013


Doesn't -n0 read zero lines from the tail of the file? You might change that value to some positive value, unless I misunderstand the docs.
posted by Blazecock Pileon at 11:03 PM on July 9, 2013


Best answer: aha.
You want 'grep --line-buffered "Keyword"' in your xargs version.
posted by spbmp at 11:08 PM on July 9, 2013


Response by poster: Blazecock: -n0 should just not prefill with old contents, unless I'm mistaken.

spbmp: thanks for the line-buffered, the issue now with that version is that it dumps out each item in the line to the other script. So I end up with like 15 notifications on my phone, one for everything on the line that grep gets.
posted by TimeDoctor at 12:52 AM on July 10, 2013


Best answer: Does it help to add -d'\n' to the xargs invocation?
posted by wachhundfisch at 1:00 AM on July 10, 2013


You can set xargs to work on one line at a time with the -l option.
$ (echo foo bar; echo iggy blah) | xargs echo
foo bar iggy blah
$ (echo foo bar; echo iggy blah) | xargs -l echo
foo bar
iggy blah
$

posted by vasi at 1:00 AM on July 10, 2013



tail -Fn0 /path/to/log/file.log |
  while read line
    do
      if echo "$line" | grep -q "keyword"
        then
          echo "matched: $line"
        else
          echo "did not match: $line"
      fi
    done

# or....
tail -Fn0 /path/to/log/file.log | while read line; do
  grep -q "keyword" <<<"$line" && echo "match $line" || echo "nomatch $line"
done


posted by zengargoyle at 6:39 AM on July 10, 2013


Golf! (save a `grep`)

tail -Fn0 blah | while read line; do
  [ "${line/keyword/}" != "$line" ] && echo "match $line" || echo "nomatch $line"
done

posted by zengargoyle at 7:14 AM on July 10, 2013


My xargs (on a mac) doesn't take -d or -l (lowercase el)
But I can do what vasi or wachhundfisch suggest with (capital) -L 1 (and that's a numeral 1)
posted by spbmp at 8:07 AM on July 10, 2013


« Older Company that fixes wonky mp3 tags?   |   Is it worth it to spring for certified organic... Newer »
This thread is closed to new comments.