blank.gif (43 bytes)

Church Of The
Swimming Elephant

Search:
Index | Description | Usage | Options | Variables | Hints | Bulletproof

expect - description of command
summary of options

If a pattern matches, then the corresponding body is executed.  expect returns the result of the body (or the empty string if no pattern matched). 

In the event that multiple patterns match, the one appearing first is used to select a body.

Each time new output arrives, it is compared to each pattern in the order they are listed. Thus, you may test for absence of a match by making the last pattern something guaranteed to appear, such as a prompt. 

In situations where there is no prompt, you must use timeout (just like you would if you were interacting manually).

Patterns are specified in three ways.  By default, patterns are specified as with Tcl’s string match command.  (Such patterns are also similar to C-shell regular expressions usually referred to as “glob” patterns). 

Any pattern beginning with a “-“ should be protected this way.  (All strings starting with “-“ are reserved for future options.)

For example, the following fragment looks for a successful login.  (Note that abort is presumed to be a procedure defined elsewhere in the script.)

expect {
    busy {puts busy\n ; exp_continue}
    failed abort
    “invalid password” abort
    timeout abort
    connected
 }
Quotes are necessary on the fourth pattern since it contains a space, which would otherwise separate the pattern from the action. 

Patterns with the same action (such as the 3rd and 4th) require listing the actions again.  This can be avoid by using regexp-style patterns (see below).  More information on forming glob-style patterns can be found in the Tcl manual.

Regexp-style patterns follow the syntax defined by Tcl’s regexp (short for “regular expression”) command.  regexp patterns are introduced with the flag -re. 

The previous example can be rewritten using a regexp as:

expect {
    busy {
        puts busy\n ;
        exp_continue
    }
    -re “failed|invalid password” abort
    timeout abort
    connected
 }
Both types of patterns are “unanchored”.  This means that patterns do not have to match the entire string, but can begin and end the match anywhere in the string (as long as everything else matches).
To anchor strings, use:
^    to match the beginning of a string
$    to match the end
Note that if you do not wait for the end of a string, your responses can easily end up in the middle of the string as they are echoed from the spawned process. 

While still producing correct results, the output can look unnatural.  Thus, use of $ is encouraged if you can exactly describe the characters at the end of a string.

Note that in many editors, the ^ and $ match the beginning and end of lines respectively.  However, because expect is not line oriented, these characters match the beginning and end of the data (as opposed to lines) currently in the expect matching buffer.  (Also, see the note below on “system indigestion.”)

See match_max for modifying the match buffer size.

If patlist is the keyword null, and nulls are allowed (via the remove_nulls command), the corresponding body is executed if a single ASCII 0 is matched.  It is not possible to match 0 bytes via glob or regexp patterns.

Upon matching a pattern (or eof or full_buffer), any matching and previously unmatched output is saved in the variable expect_out(buffer).  Up to 9 regexp sub-string matches are saved in the variables expect_out(1,string) through expect_out(9,string).

If the -indices flag is used before a pattern, the starting and ending indices (in a form suitable for lrange) of the 10 strings are stored in the variables expect_out(X,start) and expect_out(X,end) where X is a digit, corresponds to the substring position in the buffer.  0 refers to strings which matched the entire pattern and is generated for glob patterns as well as regexp patterns. 

The spawn id associated with the matching output (or eof or full_buffer) is stored in expect_out(spawn_id).

The following example waits for “connected” from the current process, or “busy”, “failed” or “invalid password” from the spawn_id named by $proc2.

expect {
    -i $proc2
    busy {
        puts busy\n ;
        exp_continue
    }
    -re “failed|invalid password” abort
    timeout abort
    connected
}
 

See also:

The -i flag may also name a global variable in which case the variable is read for a list of spawn ids.  The variable is reread whenever it changes.  This provides a way of changing the I/O source while the command is in execution.  Spawn ids provided this way are called “indirect” spawn ids.

Actions such as break and continue cause control structures (i.e., for, proc) to behave in the usual way. 


This material is excerpted from the O'Reilly book "Exploring Expect" by Don Libes, and can also be found in the manpage on many UNIX platforms.
 

Cotse.Net

Protect yourself from cyberstalkers, identity thieves, and those who would snoop on you.
Stop spam from invading your inbox without losing the mail you want. We give you more control over your e-mail than any other service.
Block popups, ads, and malicious scripts while you surf the net through our anonymous proxies.
Participate in Usenet, host your web files, easily send anonymous messages, and more, much more.
All private, all encrypted, all secure, all in an easy to use service, and all for only $5.95 a month!

Service Details

 
.
www.cotse.com
Have you gone to church today?
.
All pages ©1999, 2000, 2001, 2002, 2003 Church of the Swimming Elephant unless otherwise stated
Church of the Swimming Elephant©1999, 2000, 2001, 2002, 2003 Cotse.com.
Cotse.com is a wholly owned subsidiary of Packetderm, LLC.

Packetderm, LLC
210 Park Ave #308
Worcester, MA 01609