我有一个程序可以将信息写入stdout和stderr,并且我需要grep通过什么来到stderr,而忽略了stdout.
我当然可以分2步完成:
command > /dev/null 2> temp.file
grep 'something' temp.file
Run Code Online (Sandbox Code Playgroud)
但我宁愿能够在没有临时文件的情况下做到这一点.有没有任何智能管道技巧?
我运行这样的命令:
$ valgrind ./my_program < 1.in
Run Code Online (Sandbox Code Playgroud)
我得到Valgrind关于泄漏和错误的消息,以及my_program的输出stdout和stderr流。
我想重定向/静音my_program的所有流(stdout和stderr)。
运行> /dev/null不会使my_program的stderr流静音。
运行> /dev/null 2> /dev/null将my_program的所有输出流与Valgrind的消息一起静音。
根据此主题:如何将Valgrind的输出重定向到文件?Valgrind可以使用将输出直接流式传输到日志文件valgrind --log-file="filename"。
我想出了这样的解决方案
$ valgrind --log-file="filename" ./my_program < 1.in && cat filename
Run Code Online (Sandbox Code Playgroud)
有没有更简单的方法可以做到这一点?
我有以下两个bash脚本:
one.bash:
#!/bin/bash
echo "I don't control this line of output to stdout"
echo "I don't control this line of output to stderr" >&2
echo "I do control this line of output to fd 5" >&5
Run Code Online (Sandbox Code Playgroud)
callone.bash:
#!/bin/bash
# here I try to merge stdout and stderr into stderr.
# then direct fd5 into stdout.
bash ./one.bash 1>&2 5>&1
Run Code Online (Sandbox Code Playgroud)
当我像这样运行它:
bash callone.bash 2>stderr.txt >stdout.txt
stderr.txt文件如下所示:
I don't control this line of output to stdout
I don't control this line of …Run Code Online (Sandbox Code Playgroud)