Main Content

Buffering of inputs to expect, and match order

Archive - Originally posted on "The Horse's Mouth" - 2007-10-27 07:07:06 - Graham Ellis

If you're using the expect command to wait for one of a series of inputs within your expect program, information will be checked in the program's internal buffers inthe following order:
a) Any string that matches expect_before will be found
b) Matches to each of the possible patterns in expect in turn
c) Matches to expect_after.

Note that you can't be certain how much information will have been written to the internal buffer up to any point, so you need to exercise care when specifying the possible sequences.

Once expect_before expect_after or expect matches as string, the section up to that string is transferred to the expect_out(buffer) variable that you can reference in your continuing Tcl, and that section is removed from the internal bugger. The following expect command will start looking from where the previous successful match was found.

Note that the -notransfer option to expect will prevent the matched section from being removed from the internal buffer.

The expect_background command allows you to specifiy an expect command that runs, in effect, in an asyncronous background loop. You can use it to receive inputs from processes that may be responding from time to time while you're doing other processing in the foreground, without having to go into a wait loop until one of those background processes responds.