Expect: Automating Interactive Programs

Don Libes describes his Expect package as “a software suite for automating interactive tools.” Expect lets you drive interactive programs from a script. The shell lets you do that too, but only to a very limited extent and not in any general way. Expect lets a script feed input to commands and programs that demand their input from the terminal—meaning /dev/tty. It also allows different things to happen depending on the output it gets back, which goes far beyond what the shell offers. If this doesn’t sound like any big deal—and it didn’t to me, at first—read on and consider some of the examples in this section. Expect is actually quite addictive once you begin to figure out what it’s good for.

For more information on Expect, see its home page at http://expect.nist.gov. The book Exploring Expect, by Don Libes (O’Reilly & Associates) is also very helpful.

Conceptually, Expect is a chat script[6] generalized to the entire Unix universe. Structurally, Expect is actually an extension to another programming language called Tcl. This means that Expect adds commands—and functionality—to the Tcl language. It also means that to build and use Expect, you must also obtain and build Tcl.

A First Example: Testing User Environments

The following Expect script illustrates many of the facility’s basic features. It is used to run the /usr/local/sbin/test_user script from a user’s account. This shell script tests various security-related features of the user’s ...

Get Essential System Administration, 3rd Edition 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.