0

I have a question about the following code in TCL/EXPECT:

expect { 
    timeout { 
        puts "timeout error"
    }
    -re "Please enter the play name" {
        exp_send $common1
        exp_continue
    }
    -re "Please enter the play name_type" {
        exp_send $common2
        exp_continue
    }
    -re "Now you can get the information of the play" {
        expect {
        -re "\r"
            }
    }
    }

if I run the code above, it will be stuck at the second code block(-re "Please enter the play name_type"). why this will happen? and if I move the second code block(-re "Please enter the play name_type") on the top, the first two will pass. what is the reason?

And seems like the third code block is never executed, I added some trace inside of it like this: puts "executed!!!!" , the message never shows, and seems like it ignored the third code block and executed the code under the whole expect block, how to fix it?

4

1 に答える 1

0

Since the first pattern is a substring of the second pattern, the first pattern will match twice. When using expect -re it is commonly suggested to match up to the end of a line, so

expect { 
    timeout { 
        puts "timeout error"
    }
    -re "Please enter the play name: $" {
        exp_send $common1
        exp_continue
    }
    -re "Please enter the play name_type: $" {
        exp_send $common2
        exp_continue
    }
    -re "Now you can get the information of the play.*\r" 
}
# continue processing here

I'm making as assumption that the question ends with colon, space and end of line. Adjust accordingly.

My usual expect advice: when you're debugging or developing your program, add exp_internal 1 to the top of the script.

于 2012-06-19T20:26:29.900 に答える