tail -f | grep在if语句中



我是从bash脚本运行的,比如

$(command -options)&
export SC_PID=$!    
if tail -f <log_filename.txt> | grep --line-buffered -E "(some expression)"; then
kill -STOP $SC_PID
fi

,但它在命令行输出中写入"(some expression)",而不是杀死进程。请注意,log_filename.txt是一个日志文件,其中正在实时写入$(command -options)的输出。我做错了什么?

你的管道(tail -f ... | grep ...)永远不会结束。

-m 1添加到您的GNUgrep中,以便在第一次匹配后退出。

你的if语句"tail -f"除非你打破它,否则它不会完成,所以它无法进入下一步。尝试按行分割文本,如下所示:

$(command -options)&
export SC_PID=$!    
tail -f <log_filename.txt)|while read; do
if (echo "$REPLY"|grep --line-buffered -E "(some expression)"); then
kill -STOP $SC_PID
fi
done

最新更新