What Happens When Input Does Not Match

Once expect has matched data to a pattern, it moves the data to the expect_out variable as I showed earlier. The matched data is no longer eligible to be matched. Additional matches can only take place with new data.

Consider the following fragment:

expect "hi"
send "$expect_out(0,string) $expect_out(buffer)"

If I execute these two commands, Expect waits for me to enter hi. If I enter philosophic followed by a return, Expect finds the hi and prints:

hi phi

If I execute the two commands again, Expect prints:

hi losophi

Even though there were two occurrences of hi, the first time expect matched the first one, moving it into expect_out. The next expect started from where the previous one had left off.

With simple patterns like these, expect always stops waiting and returns immediately after matching the pattern. If expect receives more input than it needs, that input is remembered for the possibility of matching in later expect commands. In other words, expect buffers its input. This allows expect to receive input before it is actually ready to use it. The input will be held in an input buffer until an expect pattern matches it. This buffer is internal to expect and is not accessible to the script in any way except by matching patterns against it.

After the second expect above, the buffer must hold c\n. This is all that was left after the second hi in philosophic. The \n is there, of course, because after entering the word, I pressed return.

What ...

Get Exploring Expect now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.