免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6925 | 回复: 2
打印 上一主题 下一主题

请教一个expect中的$expect_out(0,string)和$expect_out(1,string)的一个问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-02-26 11:39 |只看该作者 |倒序浏览
以下是一个expect脚本,用于测试变量$expect_out(0,string)和$expect_out(1,string)
但是却遇到了一个问题,发现expect的内容并不是从最近的上一次的反馈结果取得,而是包括以前的反馈结果:
#!/usr/bin/expect
spawn /usr/bin/telnet 10.123.123.221
expect "login:"
send "gmcc\r"
expect "word:"
send "abcdef\r"
expect "@"   
send "echo \"abc 89 def\" \r "
expect -re "abc (\[0-9\]\[0-9\]) def"  
send "echo \"expect_out(0,string) in \"89\" is : $expect_out(0,string)\" \r"  <---如果存在此句,则下面得不到期待的结果
sleep 1  
send "echo \"expect_out(1,string) in \"89\" is : $expect_out(1,string)\" \r"  
#==========================================#
expect "@"  
send "echo \"abc 96 def\" \r "
expect -re "abc (\[0-9\]\[0-9\]) def"      
send "echo \"expect_out(0,string) in \"96\" is : $expect_out(0,string)\" \r"
sleep 1
send "echo \"expect_out(1,string) in \"96\" is : $expect_out(1,string)\" \r"  
interact

输出如下:
...
...
[gmcc@gmcc]$ echo "abc 89 def"
abc 89 def
[gmcc@gmcc]$ echo "expect_out(0,string) in "89" is : abc 89 def"
expect_out(0,string) in "89" is : abc 89 def   <---不知是不是由于此行的输出abc 89 def满足expect条件,影响了后面的expect
[gmcc@gmcc]$ echo "expect_out(1,string) in "89" is : 89"
expect_out(1,string) in "89" is : 89
[gmcc@gmcc]$ echo "abc 96 def"
abc 96 def
[gmcc@gmcc]$ echo "expect_out(0,string) in "96" is : abc 89 def"<---期望得到的是abc 96 def
expect_out(0,string) in "96" is : abc 89 def            
[gmcc@gmcc]$ echo "expect_out(1,string) in "96" is : 89"<---期望得到的是96
expect_out(1,string) in "96" is : 89                           


但是如果把上面的脚本中语句:
send "echo \"expect_out(0,string) in \"89\" is : $expect_out(0,string)\" \r"
注释掉,脚本运行则得到想要的结果:
...
...
[gmcc@gmcc]$ echo "abc 89 def"
abc 89 def
[gmcc@gmcc]$ echo "expect_out(1,string) in "89" is : 89"
expect_out(1,string) in "89" is : 89
[gmcc@gmcc]$ echo "abc 96 def"
abc 96 def
[gmcc@gmcc]$ echo "expect_out(0,string) in "96" is : abc 96 def"  <---得到期望值
expect_out(0,string) in "96" is : abc 96 def                        
[gmcc@gmcc]$ echo "expect_out(1,string) in "96" is : 96" <---得到期望值
expect_out(1,string) in "96" is : 96
                                                         
expect为什么会把以前的反馈结果都作为其输入呢,如何才能做到以最近上次的反馈结果作为其输入?
就是说,在最初的脚本中,既然最近的反馈结果abc 96 def 满足expect条件,那么应该
expect_out(0,string)应该为abc 96 def,而不是上次的abc 89 def
但是单独敲了expect指令进入expect1.1>之后,好像又不存在这种情况
请各位不吝赐教,谢谢啦!

论坛徽章:
0
2 [报告]
发表于 2007-02-28 15:36 |只看该作者

自己先顶一下
请求各位老大出手啊

论坛徽章:
0
3 [报告]
发表于 2007-03-05 14:16 |只看该作者
expect是怎么使的?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP