Version 35 of stderr

Updated 2003-03-26 11:20:47

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

[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 the command's 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).

glennj: 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 that takes advantage of close returning the standard error for blocking pipes:

    set somecmd {sh -c {echo "to stdout" ; echo >&2 "to stderr"}}

    set errorCode ""
    puts "no redirection:"
    set f [open "| $somecmd" r]
    set std_out [read -nonewline $f]
    catch {close $f} std_err
    puts "  std_out is {$std_out}"
    puts "  std_err is {$std_err}"
    puts "  errorCode is {$errorCode}"

    set errorCode ""
    puts "redirected:"
    set f [open "| $somecmd 2>@ stdout" r]
    set std_out [read -nonewline $f]
    catch {close $f} std_err
    puts "  std_out is {$std_out}"
    puts "  std_err is {$std_err}"
    puts "  errorCode is {$errorCode}"

The output should be:

 no redirection:
   std_out is {to stdout}
   std_err is {to stderr}
   errorCode is {NONE}
 redirected:
   std_out is {to stdout
 to stderr}
   std_err is {}
   errorCode is {}

Enrico 03-03-25: I have tried this on Windows 2000 with 8.4.1, but it doesn't work properly. The error message I've got, is

  channel "console1" wasn't opened for writing

The same result it takes, enforcing to show the console with the tcl-command console show. Redirection of stderror to a file works:

  open "| $somecmd 2> errfile.txt" r

Who have an idea - or should I use the bgexec command?

Is there someone with a critcl implementation of bgexec (like the one for busy)?


Another option of dealing with stderr, (or other file descriptors for that matter) that I have seen mentioned and used may be considered a cheat:

  exec /bin/ksh -c "command 2>&1 > /dev/null"

This provides you with the stderr on the stdout file descriptor. This specific example throws away the original stdout data. If that data is also going to be needed, then replace "> /dev/null" with "> /some/file".


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


Category Glossary