Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.* 1 > Newsgroup comp.lang.tcl

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 10-09-2009, 06:57 AM
dspfun
Guest
 
Posts: n/a
Default How to dump/print expect buffer?

Hi,

How do you dump/print whatever is in the
expect buffer to the screen?

For example:
**************************
#!/usr/bin/expect
spawn /bin/tcsh
exp_send "command1\r"
exp_send "command2 \r"
#Now I want to dump whatever is in the expect buffer to the screen,
how do I do that from this script

Brs,
Markus
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 10-09-2009, 09:41 AM
Uwe Klein
Guest
 
Posts: n/a
Default Re: How to dump/print expect buffer?

dspfun wrote:
> Hi,
>
> How do you dump/print whatever is in the
> expect buffer to the screen?
>
> For example:
> **************************
> #!/usr/bin/expect
> spawn /bin/tcsh
> exp_send "command1\r"
> exp_send "command2 \r"
> #Now I want to dump whatever is in the expect buffer to the screen,
> how do I do that from this script


<jedigesture> You don't want to know this, ..

you probably want :
set result [ exec /bin/tcsh -c "$command1 ; $command2" ]

output buffering from the spawned process ( you did [expect] nothing )
will probably block the second commands execution.
timing will wreak havok on what is in the expect buffer or still
stuck in or "before" your spawned process if you just try to expect "all"
at any time.

[log_user 1] ( the expect default ) dumps all output (from the spawned
process) traversed while trying to match an [expect]ed expression.
so, if you [expect] nothing, nothing will be dumped to the screen.
if you [expect *] you will match the _current_ content of the
match buffer. This can be all output expected or just parts due to
buffering and timing issues.

expect is for dialog with tty oriented process/programs
this is often misunderstood.


uwe





Reply With Quote
  #3 (permalink)  
Old 10-12-2009, 08:51 AM
dspfun
Guest
 
Posts: n/a
Default Re: How to dump/print expect buffer?

On Oct 9, 11:41*am, Uwe Klein <uwe_klein_habertw...@t-online.de>
wrote:
> dspfun wrote:
> > Hi,

>
> > How do you dump/print whatever is in the
> > expect buffer to the screen?

>
> > For example:
> > **************************
> > #!/usr/bin/expect
> > spawn /bin/tcsh
> > exp_send "command1\r"
> > exp_send "command2 \r"
> > #Now I want to dump whatever is in the expect buffer to the screen,
> > how do I do that from this script

>
> <jedigesture> You don't want to know this, ..
>
> you probably want :
> set result [ exec /bin/tcsh -c "$command1 ; *$command2" ]


So, since exec is a Tcl command the output of the commands executed
will not be put into any buffer, right?

When you say: "output buffering from the spawned process ( you did
[expect] nothing ) will probably block the second commands execution."

Do you mean that command2 never will be executed, or that command2
will eventually be executed?

It would seem logical if the Expect-interpreter did not continue
executing the script commands until a "send command" is completely
finished with all expect buffering also completed? Is that not the
case? I guess the reason is that it's impossible for expect to know
when a "send command" has finished.

A related question, how do you expicitly wait for command1 to finish
(so that all expect buffering is finished) in the following:

#!/usr/bin/expect
spawn /bin/tcsh
exp_send "command1\r"
#Wait for command1 and expect buffering to finish before continuing.
exp_send "command2 \r"

before I continue my script?

Is there always a risk that the output buffering from command1 will
block command2's execution?

What is the correct way to solve it when doing send of 2 different
commands back to back? Is the solution to always to do expect after
each send command so that the last string of the expected output is in
the expect command?

Thank's for great help!

Brs,
Markus
Reply With Quote
  #4 (permalink)  
Old 10-12-2009, 10:06 AM
Uwe Klein
Guest
 
Posts: n/a
Default Re: How to dump/print expect buffer?

dspfun wrote:
> On Oct 9, 11:41 am, Uwe Klein <uwe_klein_habertw...@t-online.de>
> wrote:
>
>>dspfun wrote:
>>
>>>Hi,

>>
>>>How do you dump/print whatever is in the
>>>expect buffer to the screen?

>>
>>>For example:
>>>**************************
>>>#!/usr/bin/expect
>>>spawn /bin/tcsh
>>>exp_send "command1\r"
>>>exp_send "command2 \r"
>>>#Now I want to dump whatever is in the expect buffer to the screen,
>>>how do I do that from this script

>>
>><jedigesture> You don't want to know this, ..
>>
>>you probably want :
>>set result [ exec /bin/tcsh -c "$command1 ; $command2" ]

>
>
> So, since exec is a Tcl command the output of the commands executed
> will not be put into any buffer, right?

Right, the output of [exec] has been assigned to the variable "result".

>
> When you say: "output buffering from the spawned process ( you did
> [expect] nothing ) will probably block the second commands execution."
>
> Do you mean that command2 never will be executed, or that command2
> will eventually be executed?

With a tty based application ( expect uses a pty/tty pair to talk
to a spawned process the default buffering will be linebased.
i.e. a process will read one line of input and then produce
some lines of output blocking (depending on some buffersizes 9
until that output has been read, then the next line of input will
be accessed.
Thus, until you "drain" that output ( with a spawned process via [expect ..]
the next line of input may not be handled.

The next step for the user then is to come to comp.lang.tcl and
announce he has found a bug in expect ;-)
>
> It would seem logical if the Expect-interpreter did not continue
> executing the script commands until a "send command" is completely
> finished with all expect buffering also completed? Is that not the
> case? I guess the reason is that it's impossible for expect to know
> when a "send command" has finished.

Semi Right. from expects viewpoint the exp_send .. is complete.
But the message is still in transit.
>
> A related question, how do you expicitly wait for command1 to finish
> (so that all expect buffering is finished) in the following:
>


Look at the example I wrote for your other question in another thread.

you expect some "known to come" output. From a shell that would be the
command prompt.

> #!/usr/bin/expect
> spawn /bin/tcsh
> exp_send "command1\r"
> #Wait for command1 and expect buffering to finish before continuing.
> exp_send "command2 \r"
>
> before I continue my script?
>
> Is there always a risk that the output buffering from command1 will
> block command2's execution?

yes. ( That's why sleep, after.. fixes don't work reliably )
>
> What is the correct way to solve it when doing send of 2 different
> commands back to back? Is the solution to always to do expect after
> each send command so that the last string of the expected output is in
> the expect command?

Well, I usually do it that way.

The TCLers Wiki has quite a bit of helpfull information.

http://wiki.tcl.tk/18013
http://wiki.tcl.tk/_/search?S=expect

uwe
Reply With Quote
  #5 (permalink)  
Old 10-12-2009, 10:20 AM
dspfun
Guest
 
Posts: n/a
Default Re: How to dump/print expect buffer?

On Oct 12, 10:51*am, dspfun <dsp...@hotmail.com> wrote:
> On Oct 9, 11:41*am, Uwe Klein <uwe_klein_habertw...@t-online.de>
> wrote:
>
>
>
>
>
> > dspfun wrote:
> > > Hi,

>
> > > How do you dump/print whatever is in the
> > > expect buffer to the screen?

>
> > > For example:
> > > **************************
> > > #!/usr/bin/expect
> > > spawn /bin/tcsh
> > > exp_send "command1\r"
> > > exp_send "command2 \r"
> > > #Now I want to dump whatever is in the expect buffer to the screen,
> > > how do I do that from this script

>
> > <jedigesture> You don't want to know this, ..

>
> > you probably want :
> > set result [ exec /bin/tcsh -c "$command1 ; *$command2" ]

>
> So, since exec is a Tcl command the output of the commands executed
> will not be put into any buffer, right?
>
> When you say: "output buffering from the spawned process ( you did
> [expect] nothing ) will probably block the second commands execution."
>
> Do you mean that command2 never will be executed, or that command2
> will eventually be executed?
>
> It would seem logical if the Expect-interpreter did not continue
> executing the script commands until a "send command" is completely
> finished with all expect buffering also completed? Is that not the
> case? I guess the reason is that it's impossible for expect to know
> when a "send command" has finished.
>
> A related question, how do you expicitly wait for command1 to finish
> (so that all expect buffering is finished) in the following:
>
> *#!/usr/bin/expect
> *spawn /bin/tcsh
> *exp_send "command1\r"
> *#Wait for command1 and expect buffering to finish before continuing.
> *exp_send "command2 \r"
>
> before I continue my script?
>
> Is there always a risk that the output buffering from command1 will
> block command2's execution?
>
> What is the correct way to solve it when doing send of 2 different
> commands back to back? Is the solution to always to do expect after
> each send command so that the last string of the expected output is in
> the expect command?
>


This is one way to do it:

set timeout 240
set result [spawn command1]
expect eof

or

set timeout 240
set result [spawn /bin/tcsh -c "command1;command2"]
expect eof

This will tell the Expect-interpreter to not continue until command1/
command2 has finished (via the "expect eof" )

Brs,
Markus
Reply With Quote
  #6 (permalink)  
Old 10-12-2009, 12:20 PM
dspfun
Guest
 
Posts: n/a
Default Re: How to dump/print expect buffer?

On Oct 12, 12:20*pm, dspfun <dsp...@hotmail.com> wrote:
> On Oct 12, 10:51*am, dspfun <dsp...@hotmail.com> wrote:
>
>
>
>
>
> > On Oct 9, 11:41*am, Uwe Klein <uwe_klein_habertw...@t-online.de>
> > wrote:

>
> > > dspfun wrote:
> > > > Hi,

>
> > > > How do you dump/print whatever is in the
> > > > expect buffer to the screen?

>
> > > > For example:
> > > > **************************
> > > > #!/usr/bin/expect
> > > > spawn /bin/tcsh
> > > > exp_send "command1\r"
> > > > exp_send "command2 \r"
> > > > #Now I want to dump whatever is in the expect buffer to the screen,
> > > > how do I do that from this script

>
> > > <jedigesture> You don't want to know this, ..

>
> > > you probably want :
> > > set result [ exec /bin/tcsh -c "$command1 ; *$command2" ]

>
> > So, since exec is a Tcl command the output of the commands executed
> > will not be put into any buffer, right?

>
> > When you say: "output buffering from the spawned process ( you did
> > [expect] nothing ) will probably block the second commands execution."

>
> > Do you mean that command2 never will be executed, or that command2
> > will eventually be executed?

>
> > It would seem logical if the Expect-interpreter did not continue
> > executing the script commands until a "send command" is completely
> > finished with all expect buffering also completed? Is that not the
> > case? I guess the reason is that it's impossible for expect to know
> > when a "send command" has finished.

>
> > A related question, how do you expicitly wait for command1 to finish
> > (so that all expect buffering is finished) in the following:

>
> > *#!/usr/bin/expect
> > *spawn /bin/tcsh
> > *exp_send "command1\r"
> > *#Wait for command1 and expect buffering to finish before continuing.
> > *exp_send "command2 \r"

>
> > before I continue my script?

>
> > Is there always a risk that the output buffering from command1 will
> > block command2's execution?

>
> > What is the correct way to solve it when doing send of 2 different
> > commands back to back? Is the solution to always to do expect after
> > each send command so that the last string of the expected output is in
> > the expect command?

>
> This is one way to do it:
>
> set timeout 240
> set result [spawn command1]
> expect eof
>
> or
>
> set timeout 240
> set result [spawn */bin/tcsh -c "command1;command2"]
> expect eof
>
> This will tell the Expect-interpreter to not continue until command1/
> command2 has finished (via the "expect eof" )
>
> Brs,
> Markus- Hide quoted text -
>
> - Show quoted text -


I found the following example at http://wiki.tcl.tk/11583:

RJ Here's a simpler way to catch all of the output:
proc exec_it {command} {
spawn -noecho $command
log_user 0
expect eof
return [string trimleft $expect_out(buffer) $command]
}

Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 05:02 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.