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

Actions That Affect Control Flow

So far, all I have used in the way of expect actions are commands such as set or if/then or lists of such commands. The following expect command illustrates both of these:

expect {
    a {set foo bar}
    b {
        if {$a == 1} {set c 4}
        set b 2
    }
}

It is possible to use commands that affect control flow. For example, the following while command executes someproc again and again until the variable a has the value 2. When a equals 2, the action break is executed. This stops the while loop and control passes to the next command after the while.

while 1 {
    if {$a == 2} break
    someproc
}

You can do similar things with expect commands. The following command reads from the output of the spawned process until either a 1 or 2 is found. Upon finding a 1, someproc is executed and the loop is repeated. If 2 is found, break is executed. This stops the while loop, and control passes to the next command after the while. This is analogous to the way break behaved in the if command earlier.

while 1 {
    expect {
        "2" break
        "1"
    }
    someproc
}
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.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learning Go

Learning Go

Jon Bodner
Command-Line Rust

Command-Line Rust

Ken Youens-Clark
Hands-on Ansible

Hands-on Ansible

Sander van Vugt

Publisher Resources

ISBN: 9781565920903Errata Page