当文本行出现在文件中时如何用 Bash 做一些事情 [英] How to do something with Bash when a text line appears in a file

查看:16
本文介绍了当文本行出现在文件中时如何用 Bash 做一些事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在日志文件中出现特定文本时立即运行命令.我如何在 Bash 中做到这一点?

I want to run a command as soon as a certain text appears in a log file. How do I do that in Bash?

推荐答案

使用命令

tail -f file.log | grep --line-buffered "my pattern" | while read line
do
  echo $line
done

--line-buffered 是这里的关键,否则读取将失败.

The --line-buffered is the key here, otherwise the read will fail.

这篇关于当文本行出现在文件中时如何用 Bash 做一些事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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