在 bash 脚本中,我想逐行捕获长命令行的标准输出,以便在初始命令仍在运行时对其进行分析和报告。这是我能想象的复杂方法:
# Start long command in a separated process and redirect stdout to temp file
longcommand > /tmp/tmp$$.out &
#loop until process completes
ps cax | grep longcommand > /dev/null
while [ $? -eq 0 ]
do
#capture the last lines in temp file and determine if there is new content to analyse
tail /tmp/tmp$$.out
# ...
sleep 1 s # sleep in order not to clog cpu
ps cax | grep longcommand > /dev/null
done
Run Code Online (Sandbox Code Playgroud)
我想知道是否有更简单的方法。
编辑: …