本文介绍了Linux:阻塞,直到文件中的字符串匹配(“tail + grep with blocking”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在bash / GNU工具中是否有一种单行方式阻塞,直到文件中有匹配的字符串?理想情况下,超时。我想避免多行循环。
Is there some one-line way in bash/GNU tools to block until there's a string matched in a file? Ideally, with timeout. I want to avoid multi-line loop.
更新:好像我应该强调,当字符串是匹配。
Update: Seems like I should have emphasize that I want the process to end when the string is matched.
推荐答案
感谢这两个答案,但重要的部分是该过程阻塞直到找到,然后结束。我发现这个:
Thanks both for answers, but the important part was that the process blocks until found, then ends. I found this:
grep -q 'PATTERN' <(tail -f file.log)
-q
并不太方便,但我只会使用Red Hat企业Linux,所以没关系。
并且超时:
-q
is not much portable, but I will only use Red Hat Enterprise Linux so it's ok.And with timeout:
timeout 180 grep -q 'PATTERN' <(tail -f file.log)
这篇关于Linux:阻塞,直到文件中的字符串匹配(“tail + grep with blocking”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!