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?
Thanks to glenn jackman for the idea! It seems that when called in a procedure,
expect_after
,expect_background
, and probablyexpect_before
not only look for the spawn_id which is in the global scope, but need it specified.This works:
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 aglobal spawn_id
should also work, but I ended up using this as I have about 5-6 variables, half of which I modify infoo
.