Services and Modeling for Embedded Software Development
Embecosm divider strip
Prev  Next

3.2.  The expect command

3.2.1. Patterns for use with the expect command

The general form of this command is

expect -flags pat1 body1 ... -flags patn bodyn
	

The command waits until one of the patterns matches the output of a spawned process, a specified time period has passed, or an end-of-file is seen. It then executes the corresponding body.

If (as is usual) the command takes more than one line, the arguments must be surrounded by braces. However substitutions will still occur within these braces, unlike standard TCL

A very simple example might be as follows.

expect {
    ERROR            {fail "Error encountered.\n"; }
    "Test complete"  {pass "Test completed.\n";}
    timeout          {unresolved "Timeout.\n";}
}
	

The expect command returns a result, which is the result of the body executed on a pattern match.

The expect command will be called each time there is new input. It is important to understand two aspects of expect command behavior.

  1. The expect command will skip past any unmatched text. So the above example would pass if the spawned program generated the following output.

    Computing results
    Final result is 42
    Test complete
    	    

    The first two lines don't match, so are ignored.

  2. The expect command is not line oriented. Thus the following output from the spawned program would also pass.

    FooTest completeBar
    	    
Embecosm divider strip