防止将命令发送到死进程的最佳方法是什么?
有时我的会话会在它打开时被终止,所以我最终发送命令并收到错误:
send: spawn id exp4 not open
我试图做像
这样的事情if [catch send "test\r"] {
puts "send error!"
}
但似乎每次通过时查询都是真的。
这是最简单的例子,但我有更复杂的“发送/期望”,我使用捕获组等,所以在每个“发送/期望”周围放置catch
或创建一个函数似乎没有用
你可以在整个程序中包裹catch
吗?捕获这些错误的正确方法是什么?
答案 0 :(得分:1)
Expect作者撰写的常见问题解答可解决此问题:http://expect.sourceforge.net/FAQ.html#q64
好像你想要像
这样的东西expect_before {
eof { respawning_the_process }
}
我确定会有一些皱纹被解决(比如当假设结束时该怎么办)
这个问题:
if [catch send "test\r"] {
是双重的:
catch
命令你想写:
if {[catch {send "test\r"} output] != 0} {
这可以抽象为proc
proc send {args} {
set status [catch [list exp_send {*}$args] output]
# error handling if $status is non-zero
}
" exp_send"是期望的内置别名"发送"命令,所以可以安全地覆盖"发送"使用proc,最大限度地减少所需的代码更改量。