如何使Expect程序脚本中的Expect命令等待精确的字符串匹配 [英] How to make expect command in expect program script to wait for exact string matching

查看:799
本文介绍了如何使Expect程序脚本中的Expect命令等待精确的字符串匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在Expect脚本中执行Expect命令,以等待完全匹配的字符串,然后再继续执行脚本中的下一行.

I want to know how to make expect command in expect script to wait for exact string to be matched before proceeding to next line in the script.

Bourne shell(sh)脚本:

#!/bin/sh
#!/usr/bin/expect

spawn ssh -Y localuser@lbblr-tirumala
expect -exact "localuser@lbblr-tirumala's password: "

# replace password with ur own passwd
send "geomax45\r"

# replace the prompt with ur own prompt
expect "localuser@lbblr-tirumala:~$ "

# run application
send "./sample\r"

expect "*Menu*\r
1. print hello world\r
2. print Bye world\r
3. quit\r
enter choice: "
# enter choice 1
send "1\r"
expect "Bye world\r
*Menu*\r
1. print hello world\r
2. print Bye world\r
3. quit\r
enter choice: $"
# enter choice 2
send "2\r"

在上面的代码中输入1后,代码应等待屏幕上出现"Bye world \ r ......"字符串.但是,即使屏幕完全提示了不同的字符串,该代码仍在执行下一行并输入选项2,这不应按期望的定义出现.它应该等到期望命令中的完整字符串匹配.

In the above code after entering 1 the code should wait for "Bye world\r......" string to occur on the screen. But even though the screen prompts different string altogether, the code is executing the next line and entering option 2, which should not occur as per definition of expect. It should wait until the complete string in expect command matches.

我想知道是否可以使用更严格的命令,以便期望等到确切的字符串匹配发生.请帮助我解决这个问题

I want to know whether there is any stricter command that can be used so that expect waits until exact string matching occurs. Please help me in this issue

推荐答案

您之前已经在脚本expect -exact中使用它.但是,很长的匹配字符串几乎可以保证会导致问题.您应该尝试将其缩减为最短的唯一匹配项,如有必要,可以将其分解为多个expect命令.

You're already using it earlier in your script: expect -exact. But long matching strings are pretty much guaranteed to cause problems; you should try to cut it down to the shortest unique match, if necessary breaking it into multiple expect commands.

在这种情况下,仅根据您显示的内容,正确的命令可能是expect -exact "choice:".但是从本质上讲,如果没有可能的程序输出的完整详细信息,就不可能确定这一点.

In this case, just based on what you show, the proper command is probably expect -exact "choice:". But inherently it is not possible to determine this without full details of the possible program outputs.

这篇关于如何使Expect程序脚本中的Expect命令等待精确的字符串匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆