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

What Else Gets Stored In expect_out

In this chapter and the previous one I have shown how the array expect_out is used to store strings that match parenthesized subpatterns. The expect command can also set several other elements of expect_out.

If the pattern is preceded by the -indices flag, two other elements are stored for each element expect_out(X,string) where X is a digit. expect_out(X,start) contains the starting position of the first character of the string in expect_out(buffer). expect_out(X,end) contains the ending position.

Here is a fragment of one of the earlier rsh scripts, just as it queries the remote shell for the status, but with the -indices flag added:

send "echo \$status\r"
expect -indices -re "\r\n(.*)\r\n"

The -indices flag precedes the pattern (including its type). In later chapters, you will learn about other expect flags. All of these flags follow this pattern—they precede the pattern.

With the -indices flag, the expect command implicitly makes the following assignments:

set expect_out(0,start) "12"
set expect_out(0,end) "16"
set expect_out(0,string) "\r\n0\r\n"
set expect_out(1,start) "14"
set expect_out(1,end) "14"
set expect_out(1,string) "0"
set expect_out(buffer) "echo \$status\r\n0\r\n"

These elements are set before an expect action begins executing. To be precise, the scope inside an expect action is exactly the same scope as that which contains the expect itself. In simple terms, as long as you are in a single procedure, a variable defined before the ...

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