尾部语法或grep命令错误? [英] Faulty tail syntax or grep command?

查看:55
本文介绍了尾部语法或grep命令错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已获得以下代码:

tail -fn0 /var/log/messages | \  
while read line ; do  
       echo "$line" | grep "test"  
       if [ $? = 0 ]  
       echo "Running information gathering"  
       then  
etc...etc  

它应该做的是持续监视"/var/tmp/messages"文件中添加的行,如果其中包含"test"一词,则执行脚本的其余部分并在完成后退出.
只要将任何行添加到消息文件中,它就会执行脚本的其余部分,而与行内容无关.我已经添加了echo命令,并且$ line正确包含了新的日志文件行.我尝试将测试"$?= 0"更改为"$?= 1",但这没什么区别.
有人可以给我指点吗?

What it's supposed to do is continually monitor the added lines of the "/var/tmp/messages" file and if one contains the word "test" then execute the rest of the script and exit when done.
It's executing the rest of the script as soon as any line is added to the messages file, irrespective of line content. I've added echo commands, and $line contains the new log file line correctly. I've tried changing the test "$? = 0" to "$? = 1" but it makes no difference.
Could someone please give me a pointer?

感谢@TomFenech

Thanks @TomFenech

推荐答案

我建议不要使用循环,而只需使用grep即可使事情变得简单得多.

I would suggest that instead of using a loop, you can make things a lot simpler by just using grep:

tail -fn0 /var/log/messages | grep -q test
echo "Running information gathering"
# rest of script

grep -q 在第一个匹配项后退出,因此您的脚本将在找到第一个匹配项后继续.

grep -q exits after the first match, so your script will continue as soon as the first match is found.

这篇关于尾部语法或grep命令错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆