while循环中的期望脚本 [英] while loop in expect script
问题描述
我是新来的期待脚本编写的人,我想写这样的东西:
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.
推荐答案
您可以使用exp_continue
来处理这种情况.命令exp_continue
允许期望自己继续执行,而不是像往常那样返回.这对于避免显式循环或重复的Expect语句很有用.默认情况下,exp_continue
重置timeout
计时器.如果使用-continue_timer
标志调用exp_continue
,则计时器不会重新启动.
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.
在expect
中,默认超时为10秒.即expect
将等待期望的字符串出现的时间.
In expect
, the default timeout is 10 seconds. i.e. the time till which expect
will wait for the expected string to appear.
我们以前在expect
中将期望的字符串作为
we used to give the expected string in expect
as something like
expect "name"
,它将等待字符串'name'并在发生超时时继续执行下一条语句.为了处理超时情况,我们在expect
本身中使用关键字timeout
.
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 }
}
您可以使用set
命令更改timeout
值,如下所示.
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
这是什么\033
?这是Escape键的八进制代码.然后,我们将连字符与其符号-
组合在一起,结果为\033-
.所以我们的最终代码将是
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
这篇关于while循环中的期望脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!