如何在Expect脚本中清理先前的Expect_buf [英] how to clean up the previous expect_buf in an expect script
问题描述
我已经编写了一个Expect函数来获取命令的输出,而我的代码如下所示
I've written a expect function to get the output of a command and my code is like below
proc do_cmd {cmd id} {
set spawn_id $id
send "$cmd\r"
expect "$cmd\r"
expect {
-re "\n(.*)\r\n" {return $expect_out(1,string)}
default {exit 1}
}
}
如果我只调用一次函数就可以正常工作并返回我想要的东西,但是如果我不间断地连续调用它,它将返回不需要的东西.
If I call the function just once it would works fine and return something I want, but if I call it continually without a break, it would return something unwanted.
# test case 1
set ret [do_cmd $mycmd $spawn_id]
puts "$mycmd returns $ret" # the return value is ok
# test case 2
set ret [do_cmd $mycmd $spawn_id]
set ret [do_cmd $mycmd $spawn_id]
puts "$mycmd returns $ret" # the return value is not something I want
我使用'exp_internal 1'对其进行调试,发现第二个被调用命令中的Expect_out仍然保留了先前的输出信息并引起了匹配的问题,因此,我该如何清理Expect_out缓冲区(我试图对其进行设置一个空字符串,但它不起作用,)还是我可以做些其他事情来避免此问题?预先感谢.
I use the 'exp_internal 1' to debug it and found that the expect_out in the second called command still holds the previous output info and caused the matched problem, so how can I clean up the expect_out buffer(I tried to set it an empty string but it doesn't work,) or is there anything else I can do to avoid this problem? Thanks in advance.
推荐答案
Don Libes对您的方案的建议如下,
Don Libes's suggestion for your scenario is as follows,
有时甚至说:
Sometimes it is even useful to say:
expect *
此处*匹配任何内容.这就像在说:我不在乎 在输入缓冲区中.扔掉."这种模式始终匹配,甚至 如果那里什么都没有.请记住,*匹配任何东西,而空 字符串是什么!作为此行为的必然结果,此命令 总是立即返回.它从不等待新数据到达.它 不需要,因为它可以匹配所有内容.
Here the * matches anything. This is like saying, "I don't care what's in the input buffer. Throw it away." This pattern always matches, even if nothing is there. Remember that * matches anything, and the empty string is anything! As a corollary of this behavior, this command always returns immediately. It never waits for new data to arrive. It does not have to since it matches everything.
参考资料:探索期望
在这种情况下,在您需要进行匹配之后,最好尝试将匹配保存到某个变量中,然后在最后简单地添加代码expect *
.这将清空缓冲区.您的代码可以进行如下更改.
In this case, after your required match, better try to save the match to some variable then simply add the code expect *
at the last. This will empty the buffer. Your code can altered as below.
proc do_cmd {cmd id} {
set spawn_id $id
send "$cmd\r"
#Looks like you are looking for a particular command to arrive
expect "$cmd\r"
#Then you have one more expect here which is you want to get it
expect {
#Saving the value sub match to the variable 'result'
-re "\n(.*)\r\n" {set result $expect_out(1,string)}}
}
#Causing the buffer to clear and it will return quickly
expect *
return $result
}
除此之外,还有另一种方法可以取消设置expect_out(buffer)
内容本身,这将从expect_out
数组中删除缓冲区"索引,该索引可以表示为
Apart from this, there is one more way can be unsetting the expect_out(buffer)
content itself which will remove the 'buffer' index from expect_out
array which can be depicted as
unset expect_out(buffer)
当下一次匹配发生时,expect_out
数组将被更新为索引"buffer",我们可以得到新的expect_out(buffer)
值.如果您更喜欢用这种方式,请用上面的代码替换expect *
.
When the next match happens, expect_out
array will be updated the index 'buffer' and we can have the fresh expect_out(buffer)
value. Replace the expect *
with the above code if you prefer to use this way.
这是一种变通办法,可以实际获得我们想要的东西.您可以采用任何方法.选择是你的. :)
This is quite a workaround kind of stuff to get what we want actually. You can go ahead with any approach. Choice is yours. :)
这篇关于如何在Expect脚本中清理先前的Expect_buf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!