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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can TCL execute a process without loss of stdout?

Status
Not open for further replies.

flyinal

Programmer
Feb 24, 2005
2
0
0
US
We have a clear understanding of the use of exec in TCL.

But, is there really NO WAY to use TCL to execute a process where the user will SEE stdout while the process is running?

That just seems odd. You can see people asking about this on this site and numerous others, but there is not one solution to this issue.

Just wanted to see if someone could confirm or not.
 
Yes, there is a way.

Found this in a very old wish reference, and it works with the current version of TCL:

The idea is to use exec without the catch. This can be undesired, but if you can live with it, do this:

exec x:\path\program.exe >&@stdout

TCL will wait for the program to exit, and while it is running [whatever] console you are in will show stdout.
AND, if the program returns something to stderr, you will still get a 'catch' that will stop execution and display the error.

 
Look into using the "open" command with command piplines. You can then use file event commands on the channel created and send that directly to stdout.

set chan [open | <command> <command args> r]



 
Depends on your platform.
For windows the suggestions above make sense to me.
Try this for *nix. it's whats going on behind the scenes anyway.
Code:
#!/usr/bin/tclsh
package require Tclx


#traditional
if {![llength $argv]} {error "Must give argument: name of command to execute."}
set descriptors [pipe]

   if {[set child [fork]] == 0} {
       close [lindex $descriptors 0]
       dup [lindex $descriptors 1] stdout
       dup [lindex $descriptors 1] stderr
       execl [lindex $argv 0] [lrange $argv 1 [llength $argv]]
       exit
   }      
   #parent
             close [lindex $descriptors 1]
	     set n 0
	     while {[gets [lindex $descriptors 0] line] > -1} {puts "[incr n]: $line"}
	     wait $child
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top