Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pipes and stderr

Status
Not open for further replies.

garp123

Technical User
Sep 13, 2002
2
DE
would like to evaluate the stderr msg genereated by malfunctioning call to a shell command:

Code:
set pipe [open "| rsh $host -l $user echo OK" r ]
puts [gets $pipe]

script will return OK if command works but will return an empty string otherwise.
How can I evaluate the stderr message "permission denied" if rsh failes? Any idea?
 
Most people would use expect for this.
Is there some reason why you must use plain
tcl?
 
I would like to use plain TCL to be OS independent.
Well I'll have an look to expect to solve the problem. Thank's.
 
I'd suggest going to the Tcl'ers Wiki ( and checking out the page "stderr," It provides several examples of handling the standard error channel of programs started both with exec and with open.

If all you need to do is merge the standard error channel with standard output (so that you can use gets or read to read the merged channels), the following will work:

Code:
set pipe [open "| rsh $host -l $user echo OK 2>@ stdout" r ]
puts [gets $pipe]
# etc...

If you want to keep stderr separate from stdout, don't do any redirection of stderr. Instead, use the catch command when closing the pipe. When an execed or opened program writes to its stderr channel, and you don't redirect that channel, exec or close generates an error, and the error message is the stderr output. Applying this in your case:

Code:
set pipe [open "| rsh $host -l $user echo OK" r ]
puts [gets $pipe]
catch {close $pipe} stderr_output
- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top