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, kindly help me.
You can make use of
exp_continue
to handle this situation. The commandexp_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 thetimeout
timer. The timer is not restarted, ifexp_continue
is called with the-continue_timer
flag.In
expect
, the default timeout is 10 seconds. i.e. the time till whichexpect
will wait for the expected string to appear.we used to give the expected string in
expect
as something likewhich 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
inexpect
itself.You can change
timeout
value by usingset
command as shown below.So, combining all together in one shot,
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
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,Reference : Tcl's wiki & ASCII Char Table