Which Pattern Goes With Which Spawn Id
In the chess script, it is very clear which pattern associates with which spawn id—any pattern immediately preceded by a "−i" flag is associated with the spawn id that follows. If additional patterns are specified before another "−i" flag, the additional patterns are associated with the most recently specified spawn id. Here is an example:
expect {
-i $id1
"patternX" actionX
"patternY" actionY
-i $id 2
"patternZ" actionZ
}
In the fragment above, expect waits for either patternX or patternY from spawn id $id1, or patternZ from $id2.
Patterns that appear before any −i flags are associated with the currently spawned process, described by the contents of the spawn_id variable. This is exactly what was done earlier in the telnet example.
The keyword pattern eof works just like any other pattern associated with the most recent spawn id. Here is the chess script augmented with eof patterns.
while 1 {
expect {
-i $id2 -re "\.\. (.*)\n" {
send −i $id1 $expect_out(1,string)
} eof exit
-i $id1 -re "\.\. .*\. (.*)\n" {
send −i $id2 $expect_out(1,string)
} eof exit
}
}In this example, the first "eof exit" is associated with id2 and second "eof exit" with id1. It is possible to associate the same pattern with multiple spawn ids. I will show how to do that shortly.
The timeout keyword works as before; however, it is worth pointing out that the timeout does not associate with any particular spawn id. So it does not matter where you put the timeout keyword (or ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access