Version 18 of stderr

Updated 2002-09-18 09:21:19

stderr is one of the stdio output files opened - applications and functions tend to use it for the output of error messages. no editing is permitted Ok

[Explain common idioms for management of stderr from subprocesses.]

bgexec

[ exec conventions]


Pure Tcl programs write to stderr in two ways:

  • "puts stderr $msg" gives fine-grained control;
  • "error $message" generally has the effect of reporting a diagnostic traceback to stderr (except if prevented by an outer catch, or in wish which pops up an error info window instead).

Often people ask how to open a pipeline to a command and read both its stdout and stderr.

One recent example of how to do this was:

 set fd1 [open "|somecmd |& cat" "r"]

(if your system has a command named cat in the default path).

Glenn Jackman: Or, without having to open a cat process (see http://www.tcl.tk/man/tcl8.4/TclCmd/exec.htm ):

 set fd1 [open "|somecmd 2>@ stdout" r]

Here's a quick test sequence:

 set somecmd {sh -c {echo "to stdout" ; echo >&2 "to stderr"}}
 set f [open "| $somecmd" r]
 set std_out [read -nonewline $f]
 catch {close $f} std_err
 puts "no redirection: std_out is '$std_out', std_err is '$std_err'"
 set f [open "| $somecmd 2>@ stdout" r]
 set std_out [read -nonewline $f]
 catch {close $f} std_err
 puts "redirected: std_out is '$std_out', std_err is '$std_err'"

Unfortunately, the 'cat' solution means you lose the exit status of the process whereas the non-cat solution doesn't let your script read the text, it only redirects it to the stdout channel of the tclsh process. It seems that bgexec provides the only complete solution.


See also stdout, stdin, stdio, and magic names, Tcl syntax help


What category should this page be in? "infrastructure?" "Glossary"?