Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am running valgrind to shake out bugs in my program. Valgrind's memcheck prints to stderr. What I want to do is a modification to this:

valgrind --tool=memcheck ./a.out >output 2>grindout

But, I also want valgrind output to display on screen. So program's output all goes to file. Valgrind output is placed in grindout and displayed to screen. How do I do so?

thanks.

share|improve this question
    
valgrind --tool=memcheck appname 2>&1 | tee valgrind.log from techbase.kde.org/Development/Tools/Valgrind –  hbdgaf Sep 14 '12 at 13:26
1  
Are you still looking for an answer? Did you find a solution? How are the existing answers? –  Jacob Johan Edwards Sep 30 '12 at 18:57
add comment

2 Answers 2

You're looking for the tee command. Unfortunately there's some trickery regarding redirecting stderr and stdout. The following should work:

valgrind --tool=memcheck ./a.out 3>&1 2>&3 >output | tee grindout

This redirects standard output to output, moves standard error to a new file descriptor 3, which points to stdout. tee takes this stdout-ified stderr and prints it both to your screen and to gridnout.

share|improve this answer
    
Thanks, but I want stderr to go to screen and to file. But stdout to go to file and NOT to screen. –  Badministrator Sep 14 '12 at 13:17
    
@Badministrator I've updated my answer to get that behavior. It's unexpectedly complicated (see this Stackoverflow question). –  Jacob Johan Edwards Sep 14 '12 at 13:40
add comment

Use tee for this, but you need to play some tricks with the stdout / stderr.

valgrind --tool=memcheck ./a.out 2>&1 > output | tee grindout

Explanation: 2>&1 sends stderr to stdout, and the regular stdout gets send to file output. The tee program passes on the stdin, but it also saves it to file grindout.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.