Actions That Affect Control Flow
So far, all I have used in the way of expect actions are commands such as set or if/then or lists of such commands. The following expect command illustrates both of these:
expect {
a {set foo bar}
b {
if {$a == 1} {set c 4}
set b 2
}
}It is possible to use commands that affect control flow. For example, the following while command executes someproc again and again until the variable a has the value 2. When a equals 2, the action break is executed. This stops the while loop and control passes to the next command after the while.
while 1 {
if {$a == 2} break
someproc
}You can do similar things with expect commands. The following command reads from the output of the spawned process until either a 1 or 2 is found. Upon finding a 1, someproc is executed and the loop is repeated. If 2 is found, break is executed. This stops the while loop, and control passes to the next command after the while. This is analogous to the way break behaved in the if command earlier.
while 1 {
expect {
"2" break
"1"
}
someproc
}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