0

I have following main.cpp

#include <iostream>
int main()
{
    std::cout << "Hello Geek\n";
    return 0;
}

I wrote some script to compile and run it

g++ -o test -g main.cpp
{ time ./test ; } 2>&1> /home/kj/bashTest/log

in the log I can see "Hello Geek" but the time output is displayed on console instead of log file. I am not sure how to fix it even though there is some similar post regarding this. Can anyone kindly provide me some advice?

1
  • > /home/kj/bashTest/log 2>&1 Commented Mar 15, 2020 at 10:24

3 Answers 3

2

You just need to inverse two redirections :

{ time ./test ; } > /home/kj/bashTest/log 2>&1

If you redirect standard error first, it stays on console, equivalent to having no effect.

Sign up to request clarification or add additional context in comments.

1 Comment

This is a nice answer explaining the importance of order: unix.stackexchange.com/a/37662/232207
1

A simple solution is to redirect all streams into log:

{ time ./test ; } &> /home/kj/bashTest/log

This is generally my go-to if no stream splitting is necessary.

Comments

0

This solves my question as well:

g++ -o test -g main.cpp
{ time ./test ; } 2>&1 | cat > /home/kj/bashTest/log

1 Comment

Note this solution invokes a whole additional process, and as such, may be costly for large output streams.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.