The expect Command
expect is the opposite of send. The expect command waits for a response, usually from a process. expect can wait for a specific string, but more often expect is used to wait for any string that matches a given pattern. Analogous to send, the expect command initially waits for characters from the keyboard[14]. Using them, I can create a little conversation:
expect "hi\n" send "hello there!\n"
When run, the interaction looks like this:
hi
hello there!
I typed the string hi and then pressed return. My input matched the pattern "hi\n“. Ideally, a return would be matched with "\r“; however, the UNIX terminal driver translates a return to "\n“.[15] As you will see later on, it is rarely necessary to have to worry about this mapping because most of Expect’s interactions occur with programs not users, and programs do not “press return”. Nonetheless, it is occasionally useful to expect input from people. Plus, it is much easier to experiment with Expect this way.
If expect reads characters that do not match the expected string, it continues waiting for more characters. If I had typed hello followed by a return, expect would continue to wait for "hi\n“.
When the matching string is finally typed, expect returns. But before returning, expect stores the matched characters in a variable called expect_out(0,string). All of the matched characters plus the characters that came earlier but did not match are stored in a variable called expect_out(buffer). expect does this every time ...