How can I pipe stderr, and not stdout?

后端 未结 11 1363
-上瘾入骨i
-上瘾入骨i 2020-11-22 03:28

I have a program that writes information to stdout and stderr, and I need to process the stderr with grep, leaving

11条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 04:14

    For those who want to redirect stdout and stderr permanently to files, grep on stderr, but keep the stdout to write messages to a tty:

    # save tty-stdout to fd 3
    exec 3>&1
    # switch stdout and stderr, grep (-v) stderr for nasty messages and append to files
    exec 2> >(grep -v "nasty_msg" >> std.err) >> std.out
    # goes to the std.out
    echo "my first message" >&1
    # goes to the std.err
    echo "a error message" >&2
    # goes nowhere
    echo "this nasty_msg won't appear anywhere" >&2
    # goes to the tty
    echo "a message on the terminal" >&3
    

提交回复
热议问题