当文本行出现在文件中时,如何使用Bash执行某些操作

ket*_*rin 4 linux bash grep

我想在日志文件中出现某个文本时立即运行命令.我怎么用Bash做到这一点?

ket*_*rin 14

使用命令

tail -f file.log | grep --line-buffered "my pattern" | while read line
do
  echo $line
done
Run Code Online (Sandbox Code Playgroud)

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


Bru*_*ine 5

仅使用tail:

tail -f file.log | while read line; do if [[ $line == *text* ]]; then
    mycommand
fi; done
Run Code Online (Sandbox Code Playgroud)