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)