Stderr

What is it for ?

The DOS shell, command.com, offers no mean to redirect stderr. Because of that, it's impossible to save to a file, or pipe into a program, the messages that a program prints to stderr. "foo >out" only redirects stdout of foo, not stderr. The latter still goes to the console.

Since stderr is typically where important error messages are printed, this is extremely unfortunate, as this makes us unable to detect errors in unattended jobs.

So what do you do ? One solution is to switch to Unix, where the shell allows one to redirect stderr. With sh and its derivatives, use

foo >out 2>&1
to save both stdout and stderr of foo to out. Similarly, use
foo 2>&1 | bar
to pipe both stdout and stderr of foo into bar. If you're stuck with DOS, Stderr is what you need.

Stderr runs a command and merges its stderr into its stdout. Therefore, redirecting or piping stdout is equivalent to doing the same thing to stderr as well.

Examples

To redirect stdout and stderr of foo *.txt to out, use

stderr foo *.txt >out

To pipe stdout and stderr of foo *.txt into bar, use

stderr foo *.txt | bar

Rule of thumb : type the command as you would normally do, but prepended by "stderr".

Considerations

Stderr can run any type of command, be it an executable or a batch file, with any number of arguments. Since the command is run through the shell, the environment variable COMSPEC must be set (it always is). Since the command is a child process of Stderr, Stderr (and the resident part of the shell) remain in memory during the execution of the command. This means that there is less memory available to the command than if it was run directly through the top-level shell. In the case of command.com, that's about 11 kB less.

Stderr is a good example application of the frequently misunderstood dup2 system call.

Microsoft C 5.1 included a program named errout. Stderr and errout are not equivalent. errout can redirect stdout and stderr to different files; Stderr cannot. Stderr can pipe stderr into another program; errout cannot. errout consumes more command-line real estate (at minimum 4 more characters) and more memory (73 kb).

Bugs

I think it could be made leaner if it was rewritten in assembler and linked as a .COM. A C version would also be useful, for different reasons (educational purposes).

Download

Legal

Stderr is in the public domain. Use it entirely at your own risk.


AYM 1999-10-01