相关疑难解决方法(0)

如何管stderr,而不是stdout?

我有一个程序可以将信息写入stdoutstderr,并且我需要grep通过什么来到stderr,而忽略了stdout.

我当然可以分2步完成:

command > /dev/null 2> temp.file
grep 'something' temp.file
Run Code Online (Sandbox Code Playgroud)

但我宁愿能够在没有临时文件的情况下做到这一点.有没有任何智能管道技巧?

bash grep stdout pipe stderr

933
推荐指数
8
解决办法
38万
查看次数

如何重定向正在运行Valgrind的程序的输出?

问题背景:

我运行这样的命令:

$ 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 valgrind

3
推荐指数
2
解决办法
4567
查看次数

bash文件描述符重定向

我有以下两个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)

bash

1
推荐指数
1
解决办法
138
查看次数

标签 统计

bash ×3

grep ×1

pipe ×1

stderr ×1

stdout ×1

valgrind ×1