Predefined Spawn Ids

Three variables contain spawn ids predefined by Expect. These do not correspond to actual processes, but can be logically used as if they do. They are:

user_spawn_id    standard input and standard output
error_spawn_id    standard error
tty_spawn_id    controlling terminal (i.e., /dev/tty)

user_spawn_id contains a spawn id associated with the standard input and standard output. When spawn_id is set to the value of user_spawn_id, expect reads from the standard input, and send writes to the standard output. This is exactly what happens when Expect is started, before any processes have been spawned.

set spawn_id $user_spawn_id
expect -re "(.*)\n"      ;# read from standard input

tty_spawn_id contains a spawn id associated with the controlling terminal. Even if the standard input, standard output, or standard error is redirected, the spawn id in tty_spawn_id still refers to the terminal.

set spawn_id $tty_spawn_id
expect -re "(.*)\n"      ;# read from /dev/tty

With these spawn ids, you can view the user running the Expect script as a process. The user can be sent input and provides output to the Expect script, just like a process. While users are less reliable (usually), they can effectively be treated just like a process when it comes to interacting with them from Expect. Viewing processes as users and vice versa works well and can be quite handy. Because of this, algorithms do not have to be rewritten depending on from where input comes or output goes.

In the case that input and output ...

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.