Creating Script-less Expect Programs

Expect normally uses a script to control its execution. This means that to run an Expect application, you need both an Expect interpreter and a script.

It is possible to combine the script and the interpreter together, producing a single executable that does not depend on any other files. This executable can be copied to new machines. The machines must be binary compatible and the script must make sense on the new machine. For example, programs that are spawned by the script must exist on the new machine. Stand-alone executables that run a particular script are often called compiled, although that is not the usual definition of the word.

Compiled scripts are large. Each one must contain the script plus the executable code for Expect. If you use a single Expect application on a computer, then a compiled script makes sense. If you use more than one Expect application, compiled scripts are just a waste of space. Other than space, there are no significant differences in functionality between compiled scripts and file-based scripts.

To create a compiled Expect script using the exp_main_exp.c template, replace all of the calls to the Expect interpretation phase (such as exp_interpret_cmdfilename) with a call to Tcl_Eval. As an argument to Tcl_Eval, pass the string representing the file contents.

Tcl_Eval(interp,cmdstring);

The string representation of the command must be in writeable memory. One way to guarantee this is to declare it in the following style: ...

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.