Hi,
I am writing an expect script, but i have come into a problem ... I have two different answers to one command.
This is fine, but I do not know when I will see which answer - Is there anyway I could put an expect answer into an if statement, like so:
if { expect == "1" } {
send "working\n"
else
send "bye\n"
}
any ideas how I can get past this - it seems like a simply enough query, but I have searched google and found nothing ... any idea??
thanks ..
Radar
March 5, 2009, 7:28pm
2
I think you're looking for this
Pattern-Action Pairs
Expect also allows association between a command and a pattern. The association is made by listing the action (also known as command) immediately after the pattern in the expect command itself. Here is an example of pattern-action pairs:
expect "hi" { send "You said hi\n" }
"hello" { send "Hello yourself\n" }
"bye" { send "Good-bye cruel world\n" }
This command looks for "hi", "hello", and "bye". If any of the three patterns are found, the action immediately following it gets executed. If there is no match and the default timeout expires, expect stops waiting and execution continues with the next command in the script.
This behaves much like if, else
Expect Scripting, Turorial Expect Scripts
Hi Radar,
Thanks for your quick reply - however I have seen this example before and it isn't really what I am looking for.
Depending on my answer, I have different questions to ask ... That is why I was looking for an if statement.
if scenario 1 do this, otherwise do the other ...
Using your example, can I do this:
expect "hi" { send "You said hi\n" }
expect "Yes, I said hi" send ....
"hello" { send "Hello yourself\n" }
expect "Yes, I said hello" send ....
"bye" { send "Good-bye cruel world\n" }
expect "Good luck" send ....
Thanks again for your help
Better off with:
expect {
-re "(.)hi" { perform action }
-re "(. )hello" { perform action }
-re "(.*)bye" { perform action }
}
With a -re it will match to anything entered that has "hi" or "hello" or "bye" in it ....