O'Reilly logo

Exploring Expect by Don Libes

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Simple Patterns

Like the expect command, the interact command can execute actions upon detecting patterns. However, interact and expect behave very differently in many respects.

The syntax for specifying patterns and actions is similar to expect. Patterns and actions are listed as pairs of arguments. For example, the following interact command causes the date to be printed if ~d is typed by the user.

interact "~d" {puts [exec date]}

By default, a matched pattern is not sent on to the process. (Later, I will show how to change this behavior.) Thus, in this example, the process never sees the ~d.

Unlike the expect command, interact continues after matching a pattern and executing an action. interact continues shuttling characters back and forth between the user and process. It also continues matching patterns. In the example above, the ~d pattern can be matched again and again. Each time, the action will execute.

As with the expect command, additional pattern-action pairs may be listed. Also, all the arguments may be surrounded by a pair of braces, provided they do not all appear on the same line as the command. The -brace convention from the expect command is also supported. Here are two ways of expressing the same command:

interact "~d" {exec date} "~e" {eproc}


interact {
    "~d"        {exec date}
    "~e"        {eproc}
}

There are all sorts of useful effects that can be accomplished with these patterns. A very simple one is translating characters. For example, one of the problems with the UNIX terminal ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required