Capturing output from exec'ed command 
Author Message
 Capturing output from exec'ed command

I've just written my first tcl script, and have a question.  The
answer is almost certainly simple, but I can't seem to figure it
out.

What I've written is a simple GUI interface to a package I've
written.  The first thing it does is pop up a control panel with
three buttons, "Build", "Run", and "Quit".  When you press the
"Build" button, it pops up another window with various options
that you choose.  Then you hit "OK", and what I want it to do
is spawn a gmake, and I'd like to capture the output from gmake
and show it in another window.

Here's the little proc that runs gmake:

proc Make {} {

    global name bytes_per_real type target

    set target $name
    append target $bytes_per_real
    if {$type == "debug"} {
        append target "_g"
    }

    eval exec gmake $target

Quote:
}

Now this seems to work (i.e. it does build the package).  But where
does the output from the gmake go?  I'd like to capture it and
display it to the user as the make progresses.

Sorry to ask what must be a pretty simple question.

Thanks for any help...

----------------------------------------------------------------------------
|John A. Turner         |"Music is the cup which holds the wine of silence;|
|Los Alamos Natl. Lab.  |  sound is that cup, but empty;                   |

|                       |                        - Robert Fripp            |
----------------------------------------------------------------------------



Sat, 26 Apr 1997 04:48:53 GMT  
 Capturing output from exec'ed command
A followup to my own question.

It turns out the output from the exec'ed command *does* finally
show up after the command finishes, but in an error box.

Problems with this are:

- it's only the last bit of the output (I'd like to capture it
  all in a window with scrollbars, etc.
- it'd be nice to see what's happening as the make progresses
- wish seems to think it's an error

-John



Sat, 26 Apr 1997 04:59:29 GMT  
 Capturing output from exec'ed command

Quote:
>I've just written my first tcl script, and have a question.  The
>answer is almost certainly simple, but I can't seem to figure it
>out.

>What I've written is a simple GUI interface to a package I've
>written.  The first thing it does is pop up a control panel with
>three buttons, "Build", "Run", and "Quit".  When you press the
>"Build" button, it pops up another window with various options
>that you choose.  Then you hit "OK", and what I want it to do
>is spawn a gmake, and I'd like to capture the output from gmake
>and show it in another window.

>Here's the little proc that runs gmake:

>proc Make {} {

>    global name bytes_per_real type target

>    set target $name
>    append target $bytes_per_real
>    if {$type == "debug"} {
>    append target "_g"
>    }

>    eval exec gmake $target
>}

>Now this seems to work (i.e. it does build the package).  But where
>does the output from the gmake go?  I'd like to capture it and
>display it to the user as the make progresses.

It is the return value of exec, and hence your procedure Make.

==========================================================================
* Gerald W. Lester                        !   Voice:  (504)-889-2784     *
* Computerized Processes Unlimited        !   FAX:    (504)-889-2799     *

* Metairie, LA  70001                     !   Hours:  09:00-17:00 CDT    *
==========================================================================



Sun, 27 Apr 1997 11:38:58 GMT  
 Capturing output from exec'ed command

Quote:
> >       ...
> >    }

> >    eval exec gmake $target
> >}
> It is the return value of exec, and hence your procedure Make.

But only if 'gmake' exists normal (exit code 0)
else you will have an error-popup

instead of "eval exec gmake $target"
try        "catch {eval exec gmake $target} output_buffer"

The return-value will be the exit-code (false/true on gmake result)
and all bulding output (stdout) collected in 'output_buffer'



Mon, 28 Apr 1997 04:00:18 GMT  
 Capturing output from exec'ed command
Others already explained that exec returns the stdout from your command
and unless your command exits with value 0, exec thinks the command
encoutered an error.


Quote:

> - it'd be nice to see what's happening as the make progresses

In that case there are two possibilities:

1)

run gmake at the end of a pipe, like this:
set cmd [open "|gmake $arguments" r]

then enter a loop in which you read a line from gmake and enter it in a
window:

while ![eof $cmd] {
  $textw insert end [gets $cmd]
  update idletasks

Quote:
}

close $cmd

.... of something like that

2)

The second possibility is much better but requires more work.  You'll
have to build a command that forks gmake at the end of a pipe and sets
up a Tk_CreateFileHandler() callback to be invoked whenever there is
stuff to be read from the pipe. The callback can then be used to insert
the stuff from the pipe to the text widget.

I've done things like this both ways.  I usually have done it using the
first way for the prototype and for the production version I've done it
the second way.
--
<A HREF="http://www.cs.hut.fi/~sti/">Sami Tikka</A>
"Peace and Long Life."
"Live Long and Prosper."



Fri, 02 May 1997 14:22:55 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. SUMMARY (followup): capturing output from exec'd command

2. SUMMARY: capturing output from exec'd command

3. ? capturing output from the exec command ?

4. Redirecting output of a exec'ed application

5. Q: interacting with exec'ed commands ?

6. Problem with exec command: error reading output from command: interrupted

7. capture output from exec

8. capture 'system' output

9. Using import in an exec'ed string

10. return val of exec'ed prog

11. interaction with exec'ed process

12. How to pass arguments to program been exec'ed

 

 
Powered by phpBB® Forum Software