user419534
user419534

Reputation: 619

How to handle different output inside expect block

Inside expect script, program can give different outputs based on system configuration. I need to handle the output inside expect block for different output and provide input accordingly to proceed with program execution. Is there some way, expect block can be written so that it can find matching patten and proceed and rest other patterns are ignored?

Sample script 

    spawn myprogram
     expect {
                        -re "pattern1" {send -- "str1 \r"}
                        -re "pattern2" {send -- "str2 \r"}
                        -re "pattern3" {send -- "str3 \r"}
               }

Here based on pattern in the output, action need to be taken.

Upvotes: 1

Views: 373

Answers (1)

Z4-tier
Z4-tier

Reputation: 7998

Are you using those double dashes to force the argument to send to be interpreted as a string? If so, I think you need spaces after the -- and before the string:

expect {
                -re "pattern1" {send -- "str1 \r"}
                -re "pattern2" {send -- "str2 \r"}
                -re "pattern3" {send -- "str3 \r"}
       }

without the space it seems to want to treat everything after the first - as one big flag.

To answer your question, yes, this looks like a workable starting point do do what you're asking for: it will watch the output from testscript and wait until one of the regular expressions matches, or until the timeout expires (since you are not setting a timeout period, it will default to 10 seconds) or until EOF is received. If one of the regex matches, it will branch into code section that follows the matched expression.

Be careful with the TCL regex syntax, if you are used to PCRE or Vim...

Upvotes: 1

Related Questions