bash 将 C++ 生成的 cerr 和 cout 重定向到 Ubuntu 中的同一个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23330421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:19:24  来源:igfitidea点击:

Redirecting C++ generated cerr and cout to the same file in Ubuntu

c++bashcoutstderrredirect

提问by gnometorule

My program generates output both directed to std::cout and std::cerr. When I run a test file normally as in

我的程序生成的输出都指向 std::cout 和 std::cerr。当我正常运行测试文件时

./a.out <source> &> <target>  

target captures both types of output in the order produced by ./a.out.

target 按照 ./a.out 生成的顺序捕获两种类型的输出。

When trying to automate testing of output for a simple unit testing framework, I implemented the above in a bash script:

在尝试为简单的单元测试框架自动测试输出时,我在 bash 脚本中实现了上述内容:

 `rm $OUT_NAME`
 `./a.out $NEW_UNIT &> $OUT_NAME`   

(with the obvious variable names). The output sent to cout is printed fine; but the one sent to cerr is printed incorrectly (some is printed; then printing stops with no error). In case you wonder, I added the 'rm' first just to be perfectly sure it's no issue with over-writing/appending to an old version.

(带有明显的变量名称)。发送到 cout 的输出打印得很好;但是发送到 cerr 的那个打印不正确(有些打印了;然后打印停止,没有错误)。如果您想知道,我首先添加了 'rm' 只是为了完全确定覆盖/附加到旧版本没有问题。

Any ideas?

有任何想法吗?

My system: Ubuntu 12.04.

我的系统:Ubuntu 12.04。

回答by unxnut

In bash, you should do the following:

在 bash 中,您应该执行以下操作:

./a.out source > target 2>&1

to merge stderrinto stdout. The command you gave is meant for csh.

合并stderrstdout. 您给出的命令适用于csh.

And if you want to merge stdoutinto stderr, you will do

如果你想合并stdoutstderr,你会做

./a.out source 2> target 1>&2

回答by Chris Cheng

./a.out source >> target 2>&1would solve your problem
2>&1means that combine(or called redirect) standard error (stderr) with standard out (stdout)
>>means to attach the output stream of your program to then end of "target". And if you use a single >instead of >>, it means to replace the output stream of your program with the "target".

./a.out source >> target 2>&1将解决您的问题
2>&1意味着将标准错误 (stderr) 与标准输出 (stdout) 结合(或称为重定向)
>>意味着将程序的输出流附加到“目标”的末尾。如果您使用单个>而不是>>,则意味着将程序的输出流替换为“目标”。

P.S. Suppose your code have two different output
std::cout<<"results"<<endland some std::cerr<<"debug information"<<endl

PS假设您的代码有两个不同的输出
std::cout<<"results"<<endl和一些std::cerr<<"debug information"<<endl

In bash, you can do ./a.out source 1>>Result.txt 2>> Debug.txt
And your "results" would be store in Result.txt and your "debug information" would be store in Debug.txt.
1>>means standard output redirect to the back of somewhere, you can also use 1>which means standard out redirect to somewhere
2>>means standard error redirect to the back of somewhere, you can also use 2>which means standard error redirect to somewhere

在 bash 中,您可以执行 ./a.out source 1>>Result.txt 2>> Debug.txt
您的“结果”将存储在 Result.txt 中,而您的“调试信息”将存储在 Debug.txt 中。
1>>表示标准输出重定向到某个地方的后面,您也可以使用1>这意味着标准输出重定向到某个地方
2>>意味着标准错误重定向到某个地方的后面,您也可以使用2>这意味着标准错误重定向到某个地方

That would be very useful if you want to use script automatically run your program.

如果您想使用脚本自动运行您的程序,这将非常有用。

回答by R Sahu

In bash, this should work:

在 bash 中,这应该有效:

./a.out source >target 2>&1