Waiting From Multiple Processes Simultaneously

By using multiple −i flags in a single expect command, it is possible to wait for different processes simultaneously. The following fragment executes ftp_action if an ftp process sends its prompt, or it executes the shell action if the shell sends its prompt.

expect {
    -i $ftp "ftp> " {ftp_action}
    -i $shell $prompt {shell_action}
}

At most one of the actions can be executed. After the action, control passes to the next line in the script. This is very similar to prior expect commands.

There is an input buffer associated with each spawn id. So any output from ftp is kept separate from that of the shell. Of course, when the output(s) appear on the terminal, they are mixed together, but that is just what you would see at the terminal yourself while running multiple processes simultaneously. Usually, this is not a problem. But if you do not want to see the outputs mixed together, expect one spawn id at a time. Alternatively, use log_user 0 to disable the normal output, and then explicitly write the output by calling "send_user expect_out(buffer)" when convenient.

If some but not all of the ftp prompt appears and the entire shell prompt appears, shell_action will execute. The beginning of the ftp prompt will remain in the input buffer for the next expect command.

Patterns corresponding to a single −i flag are matched sequentially as before. Because multiple processes are never active simultaneously, patterns with different −i flags are not ordered. ...

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.