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 ...

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.