Line Versus Character-Oriented And Other Terminal Modes

In all of the examples of expect_user so far, the script does not actually get to see any user input until a return is pressed. This is a natural result of the terminal interface being line-oriented. By default, the terminal driver buffers all the keystrokes until a return is pressed. Only when the return is pressed are the characters delivered to the script. While characters are being buffered, the terminal driver performs some minimal processing of the keystrokes such as echoing them, and erasing them if you press backspace or delete. By doing this processing in the terminal driver, many programs are drastically simplified while still providing a minimally intelligent user-interface.

If the following fragment is used to get input, the user can fix corrections by erasing characters and retyping up until a return is pressed. The value of expect_out will not have any of the editing characters that were entered to fix typos.

send "Enter your name: "
expect_user "\n"

All the examples so far have ended patterns from the standard input with a "\n“. In line-oriented mode, however, there is nothing else with which lines can end, so it is really just a formality. If the pattern has no "\n“, expect_user will still wait for the return to be pressed before attempting any matches.

It is possible to put the terminal into a mode in which it is character-oriented. In this case, expect_user does not wait until a return has been pressed. expect_user ...

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.