Determine if output is stdout or stderr

226

Solution 1

There are only three ways I know of to determine what a program will output to STDOUT and what to STDERR

  1. Read the documentation. Or

  2. Experiment with redirection†

  3. print STDERR in red

†For example:

program > program.stdout 2> program.stderr

Then look at the two output files to see what the program has written to STDOUT and what it has written to STDERR.

Instead of redirection you can pipe to tee if you need output to continue to the screen as well as into a file. See https://stackoverflow.com/q/692000/477035

Solution 2

Based on your commented request:

{ { command; } 2>&3 | sed 's/^/STDOUT: /'; } 3>&1 1>&2 | sed 's/^/STDERR: /'

Solution 3

You could just redirect stderr to a file and if anything shows up in it, it's from stderr.

e.g. ls -a 2> ls-all.txt

if there was an error for any reason sent to stderr, it will be redirected to this file.

Share:
226

Related videos on Youtube

Sunil
Author by

Sunil

Updated on September 18, 2022

Comments

  • Sunil
    Sunil over 1 year

    I'm using Telerik winform controls with VS2015. I add controls manually in toolbox. I can work as far as VS is running. When I close VS then they disappear. Next time to work with them I need to add again. I've faced same problem before with infragistics controls. Is there any solution that keeps controls sticking in toolbox all the time?

  • Rauffle
    Rauffle almost 12 years
    The process creates a constant stream of output, some to stdout some to stderr. I want to determine which is which as this output is going to the screen
  • Dennis
    Dennis almost 12 years
    I usually use a variation of option 3: program | grep . prints STDOUT in red.
  • Izzy
    Izzy almost 12 years
    Looks impressive. But would you mind to add the cherries to your tree, and explain what it is doing? Not everybody around here is a guru -- and your construct is quite a little complex ;)
  • zebediah49
    zebediah49 almost 12 years
    The brackets are for ordering. I actually got that exact form from I-forget-where, but the point is to use extra file descriptors (beyond 1=stdout and 2=stderr) to take the output of the inner set of brackets, and run stdout through one sed command, while stderr goes through a different one.
  • Izzy
    Izzy almost 12 years
    Wow. Didn't know to use those extra descriptors. First I was a bit confused (I like to understand what I run -- and got a bit confused with the curly braces). But now it's clear -- and IMHO exactly what the OP wanted. So +1 from me :)
  • Bryan Drewery
    Bryan Drewery about 11 years
    This helped me. I wanted to add more data into stderr. This does that and properly outputs everything back on stdout/stderr. { { { { echo "stdout" ; echo "stderr">&2; } 2>&3; } 3>&1 1>&2 | awk '{print "ERROR:",$0}' 1>&3; } 3>&2 2>&1; }