Re: How to control the loop within expect.




Bruce Hartweg 写道:

hujunfeng@xxxxxxxxx wrote:
I just met a problem when I have a loop and an expect in this loop.
for example
for {set i 1} {i<10} {incr i} {
send a commanda
expect {
1 {puts 1}
2 {puts 2}
3 {........}
}
send a commandb
}
when I get the value 1, I will puts 1 and execute the commandb, so as
2.but my question is if I get the value 3, I want to continue the loop
but not execute send the commandb.
I've used break or exit , these all seemed exit the loop.
Do anybody knows this?
Thanks.

2 things,
As others have mentioned you can use expect itself as the looping
construct (i.e. if you want to keep doing the same interactions until some
condition of the interaction makes you want to stop. in this case you
just add exp_continue to each branch that doesn't want you to stop.

If the above example is actually what you are wanting (i.e. the loop condition
is *external* to your interaction, and you have other code in your loop
in addition to the expect, you can use the normal tcl continue to stop
the current loop body and continue to the next one

for {set i 1} {i<10} {incr i} {
send a commanda
expect {
1 {puts 1}
2 {puts 2}
3 {
........
continue
}
}
send a commandb
}



Bruce

Thanks, it works:)
But I find another problem with it ,if I put the continue in a
procedure, it will not works again. for example:

proc CMD {con} {
upvar CMDSUCC CMDSUCC
upvar CMDFAIL CMDFAIL
send -i $con "cd /nodes\r"
expect {
-i $con
$CMDSUCC {
puts "succe"
continue
}
$CMDFAIL {
puts "fail"
continue
}
}
puts "123"
}



set i 0
if {$i==0} {
foreach $i {1 2 3 4 5} {
CMD $CLICom
}
} else {
puts "hehe"
}


the result is
invoked "continue" outside of a loop

.



Relevant Pages

  • Re: How to control the loop within expect.
    ... when I get the value 1, I will puts 1 and execute the commandb, so as ... but not execute send the commandb. ... these all seemed exit the loop. ... If the above example is actually what you are wanting (i.e. the loop condition ...
    (comp.lang.tcl)
  • How to control the loop within expect.
    ... I just met a problem when I have a loop and an expect in this loop. ... when I get the value 1, I will puts 1 and execute the commandb, so as ... but not execute send the commandb. ... I've used break or exit, these all seemed exit the loop. ...
    (comp.lang.tcl)
  • Re: Getting working code to work with socket communications
    ... the puts statement is not execute UNLESS ... The vwait line above means "stop here until the variable do_the_action ... Hence the following for loop is not executed UNLESS ...
    (comp.lang.tcl)
  • Re: deaf grandma.
    ... puts "Simon says, ... and check the flag each time to see whether we loop again. ... puts "that was in all caps" ...
    (comp.lang.ruby)
  • [SUMMARY] Word Loop (#149)
    ... cheater is a wonderful label for a programmer to have. ... the first priority is to find a possible loop. ... puts before+letter+looplets.shift ...
    (comp.lang.ruby)