In bash, you should do the following:
./a.out source > target 2>&1
to merge stderr
into stdout
. The command you gave is meant for csh
.
And if you want to merge stdout
into stderr
, you will do
./a.out source 2> target 1>&2
题
My program generates output both directed to std::cout and std::cerr. When I run a test file normally as in
./a.out <source> &> <target>
target captures both types of output in the order produced by ./a.out.
When trying to automate testing of output for a simple unit testing framework, I implemented the above in a bash script:
`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.
Any ideas?
My system: Ubuntu 12.04.
解决方案
In bash, you should do the following:
./a.out source > target 2>&1
to merge stderr
into stdout
. The command you gave is meant for csh
.
And if you want to merge stdout
into stderr
, you will do
./a.out source 2> target 1>&2
其他提示
./a.out source >> target 2>&1
would solve your problem
2>&1
means 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".
P.S.
Suppose your code have two different output
std::cout<<"results"<<endl
and some 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
That would be very useful if you want to use script automatically run your program.
In bash, this should work:
./a.out source >target 2>&1