问题
I'm automating some work with expect, and have something like the following:
# procedure to set background and after patterns
proc foo {} {
expect_after {
-re $regex1 {puts "Matched regex1"; send $command_to_run; exp_continue}
timeout {exp_continue}
}
expect_background {
-re $regex2 {do other things; exp_continue}
-re $regex3 {and more different things; exp_continue}
timeout {exp_continue}
}
}
spawn $thing
foo
expect_user {
-ex "stahp" {exit}
}
This hangs indefinitely after expect_after
pattern is matched (and the associated body is run). However, if I move the expect_after
and expect_background
patterns out of the procedure, then it runs as I, well, expected.
Why does it behave differently when put in a procedure?
回答1:
Thanks to glenn jackman for the idea! It seems that when called in a procedure, expect_after
, expect_background
, and probably expect_before
not only look for the spawn_id which is in the global scope, but need it specified.
This works:
proc foo {} {
namespace eval global {
expect_after {
-i $spawn_id -re $regex1 {do things}
}
expect_background {
-i $spawn_id -re $regex2 {do more different things}
-i $spawn_id ...
}
}
}
If anyone can explain why it needs -i $spawn_id
that would be great, but here's a fix for anyone running into the same problem. Adding a global spawn_id
should also work, but I ended up using this as I have about 5-6 variables, half of which I modify in foo
.
来源:https://stackoverflow.com/questions/16848126/is-behavior-of-expect-after-expect-background-in-proc-different-than-when-called