The interpreter Command

When Expect runs interactively, it prompts for a command. The command is evaluated and Expect prompts for another command. This continues, typically, until you press ^D or enter the exit command. It is possible to kill Expect in a myriad of other ways, such as by sending it a kill signal or closing the standard input but I will not describe these further here.

When Expect is interactively prompting for commands, it is actually running a command called interpreter. The interpreter command can be invoked just like any command. It takes no arguments and is invoked simply as:

interpreter

The interpreter command is often used just for experimenting with Expect. For example, you might write a dozen or so lines of code at a time and then want to test them out even though the script is not complete. Rather than just having the script exit, you can stick in an interpreter command at the end. When the script is executed, you will get control so you can see what has been accomplished and can play around a little more.

With a small modification to any script, you can use interpreter as part of a general strategy for catching any errors. Nest any executable commands at the top level inside a catch command. This technique is ideal once you have a complete script but are still running across occasional errors. Make your script look like this:

# define all procedures proc first { . . . proc second { . . . proc third { . . . # call first procedure that starts things rolling ...

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.