How can I kill a process when a specific string is seen on standard error?

后端 未结 5 1694
执念已碎
执念已碎 2021-01-05 00:01

I need to start a process, lets say foo. I would like to see the stdout/stderr as normal, but grep the stderr for string bar. Once

5条回答
  •  花落未央
    2021-01-05 00:29

    Just as an alternative to the other answer, one way would be to use bash's coproc facility:

    {coproc FOO { foo; } 2>&1 1>&3; } 3>&1
    CHILD=$!
    while read line <&${FOO[0]}; do
        if echo "$line" | grep -q bar; then
            kill $CHILD
        else
            echo "$line"
        fi
    done
    

    That's clearly bash-specific, though.

提交回复
热议问题