1

I would like to be able to

  • Display STDERR on the screen
  • Copy STDOUT and STDERR in files (and if possible in the same file)

For information, I am using Msys to do that.

.

After doing some research on the SO, I have tried to use something like

<my command> > >(tee stdout.log) 2> >(tee stderr.log)

Bu I got the following error:

sh: syntax error near unexpected token `>'

.

Any idea on how to do that?

Jean-Francois T.
  • 11,549
  • 7
  • 68
  • 107

2 Answers2

1

I have a simple script (test.sh) that generates STDOUT and STDERR:

#!/bin/bash
echo hello
rm something 
exit

Then, to do what you want execute with the following:

./test.sh > stdout.log 2> >(tee stderr.log >&2)

You'll get the STDERR on the screen, and two separated log files with STDERR and STDOUT. I used part of the answer given here

Note that I am assuming you don't have a file called something on the current directory :)

If you want both STDOUT and STDERR to go to the same file, use the -a option on tee:

./test.sh > std.log 2> >(tee -a std.log >&2)
Community
  • 1
  • 1
Vinicius Placco
  • 1,683
  • 2
  • 14
  • 24
1

There might be no direct solution in Msys but the >(tee ... ) solution works fine in *Nix, OSX, and probably Cygwin.

The workaround is to grep all the errors and warnings we want to keep them on the screen.

I have successfully used the following command for a makefile to compile C code:

make 2>&1 | tee make.log | grep -E "(([Ee]rror|warning|make):|In function|undefined)"

Jean-Francois T.
  • 11,549
  • 7
  • 68
  • 107
  • That is a good workaround. Could you, instead of using `grep -E` to get STDERR, use `grep -v` to exclude STDOUT and get everything else? Well, that assumes you know what the STDOUT is, which may not be the case. – Vinicius Placco Jun 21 '16 at 12:28
  • @ViniciusPlacco: provided you know what would be in STDOUT, you can definitely use `grep -v` ... point is, a pattern on errors and warnings is much easier :) – Jean-Francois T. Jun 22 '16 at 03:47
  • Fair enough :) Good to have both in the pocket. Thanks! – Vinicius Placco Jun 22 '16 at 10:09