while loop in expect script
Asked Answered
A

1

0

I'm new to expect scripting, I want to write something like this:

set variable;
$variable = expect -exact "\/----Enter Password----\\"
while { != $variable } {
send -- {^[-}
}

I want to keep sending escape+hyphen character until I expect this prompt:

"\/----Enter Password----\\"

I have written the above code but it is not working. How do I do this?

Aronarondel answered 12/12, 2014 at 19:1 Comment(0)
J
3

You can make use of exp_continue to handle this situation. The command exp_continue allows expect itself to continue executing rather than returning as it normally would. This is useful for avoiding explicit loops or repeated expect statements. By default, exp_continue resets the timeout timer. The timer is not restarted, if exp_continue is called with the -continue_timer flag.

In expect, the default timeout is 10 seconds. i.e. the time till which expect will wait for the expected string to appear.

we used to give the expected string in expect as something like

expect "name"

which will wait for the string 'name' and proceed to next statement if timeout happened. To handle the timeout scenario, we are using the keyword timeout in expect itself.

expect {
       "name" { # Some code here }
        timeout { # timeout_hanlder_code_here }
}

You can change timeout value by using set command as shown below.

set timeout 60; # Timeout will happen after 60 seconds.

So, combining all together in one shot,

expect { 
        # If the phrase 'Enter Password' seen, then it will send the password
        "Enter Password" {send "yourpassword\r"}
        # If 'timeout' happened, then it will send some keys &
        # 'expect' will be looped again. 
        timeout {send -- {^[-}; exp_continue}
}

Note : I am seeing a problem in your code. You have mentioned that you have to send escape + hyphen key together. But, you are sending only literal square bracket ([) and hyphen (-) symbol. If it is working then fine and you don't need to read this 'Note' section.Skip it. Else, proceed to read below.

You should send the actual Escape character to the program. It can be done as

send -- \033-; # Sending Escape + hyphen together

What is this \033 ? It is the octal code for Escape key. Then along with that we are just combining the hyphen with it's symbol as - which results in \033-. So our final code will be,

expect { 
            # If the phrase 'Enter Password' seen, then it will send the password
            "Enter Password" {send "yourpassword\r"}
            # If 'timeout' happened, then it will send some keys &
            # 'expect' will be looped again. 
            timeout {send -- \033-; exp_continue}
    }

Reference : Tcl's wiki & ASCII Char Table

Jackleg answered 13/12, 2014 at 6:5 Comment(1)
Thanks Dinesh for taking time to write the answer. I used autoexpect and whenever I entered escape-hyphen, the autoexpect script generated this symbol "^[-". Hence I thought that should be the characters. I will try what you have advised and let you know the result.Aronarondel

© 2022 - 2024 — McMap. All rights reserved.