小编gfr*_*gon的帖子

在bash脚本中,如何逐行捕获stdout

在 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)

我想知道是否有更简单的方法。

编辑: …

bash process stdout

35
推荐指数
2
解决办法
9万
查看次数

标签 统计

bash ×1

process ×1

stdout ×1