Skip to Content
Exploring Expect
book

Exploring Expect

by Don Libes
December 1994
Intermediate to advanced
606 pages
16h 7m
English
O'Reilly Media, Inc.
Content preview from Exploring Expect

Example—rogue

This previous example is a very typical Expect fragment. It does not take much more to produce a useful script. As an example, the following script provides a small assist in playing the game of rogue. rogue is an adventure game which presents you with a player that has various physical attributes, such as strength. Most of the time, the strength rating is 16, but every so often— maybe one out of 20 games— you get an unusually good strength rating of 18. A lot of rogue players know this, but no one in their right mind restarts the game 20 times to find those really good configurations—it is too much typing. The following script does it automatically:

while 1 {
    spawn rogue
    expect {
        "Str: 18" break
        "Str: 16"
    }
    send "Q"
    expect "quit?"
    send "y"
    close
    wait
}
interact

Inside a loop, rogue is started and then the strength is checked to see if it is 18 or 16. If it is 16, the dialogue is terminated. Like telnet (see Chapter 4 (p. 103)), rogue does not watch for an eof either, so a simple close is not sufficient to end the dialogue. "Q" requests that rogue quit. The game asks "are you sure" to which the script replies "y“. At this point, both the script and rogue close the connection. Then the script executes wait. As I described in Chapter 4 (p. 105), wait tells the system that it can discard the final exit status of the rogue process.

Whenrogue exits, the loop is restarted and a new game of rogue is created to test. When a strength of 18 is found, the break action is executed. ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

AI Agents in Action

AI Agents in Action

Micheal Lanham
Learning Go

Learning Go

Jon Bodner

Publisher Resources

ISBN: 9781565920903Supplemental ContentErrata Page