期望:如何从生成的进程中获取退出代码
Expect: How to get the exit code from spawned process
我不太确定创建一个新线程是个好主意还是 "reopen" this 3 岁的线程,如果您认为重新打开它更好,请原谅我的垃圾邮件在这个论坛上,但我有同样的问题,并且没有从线程的信息中得到解决方案。目前我有一个如下所示的脚本:
#!/usr/bin/expect -f
set pass [lindex $argv 0]
spawn <CMD>
expect {
-re "Password: " {
send "$pass\r"
}
expect eof
catch wait result
}
exit [lindex $result 3]
但是我执行这个脚本时出现错误
can't read "result": no such variable
while executing
"lindex $result 3"
invoked from within
"exit [lindex $result 3]"
(file "./call_tests.exp" line 13)
我已经发现问题可能是当我尝试读取退出代码时 SSH 会话已经关闭。但是我昨天开始理解 expect-command 所以我是一个完全的新手,如果有人能帮助我,我将非常感谢。
此致
旦
你应该这样写:
expect {
-re "Password: " {
send "$pass\r"
}
}
expect eof
catch wait result
exit [lindex $result 3]
或者这个:
expect {
-re "Password: " {
send "$pass\r"
exp_continue
}
eof {
catch wait result
}
}
exit [lindex $result 3]
根据 expect 的手册页:
expect [[-opts] pat1 body1] ... [-opts] patn [bodyn]
waits until one of the patterns matches the output of a spawned
process, a specified time period has passed, or an end-of-file
is seen. If the final body is empty, it may be omitted.
Patterns from the most recent expect_before
command are implicitly used before any other patterns. Patterns from the most
recent expect_after
command are implicitly used after any other
patterns.
If the arguments to the entire expect statement require more
than one line, all the arguments may be braced into one so as
to avoid terminating each line with a backslash. In this one
case, the usual Tcl substitutions will occur despite the braces.
我不太确定创建一个新线程是个好主意还是 "reopen" this 3 岁的线程,如果您认为重新打开它更好,请原谅我的垃圾邮件在这个论坛上,但我有同样的问题,并且没有从线程的信息中得到解决方案。目前我有一个如下所示的脚本:
#!/usr/bin/expect -f
set pass [lindex $argv 0]
spawn <CMD>
expect {
-re "Password: " {
send "$pass\r"
}
expect eof
catch wait result
}
exit [lindex $result 3]
但是我执行这个脚本时出现错误
can't read "result": no such variable
while executing
"lindex $result 3"
invoked from within
"exit [lindex $result 3]"
(file "./call_tests.exp" line 13)
我已经发现问题可能是当我尝试读取退出代码时 SSH 会话已经关闭。但是我昨天开始理解 expect-command 所以我是一个完全的新手,如果有人能帮助我,我将非常感谢。 此致 旦
你应该这样写:
expect {
-re "Password: " {
send "$pass\r"
}
}
expect eof
catch wait result
exit [lindex $result 3]
或者这个:
expect {
-re "Password: " {
send "$pass\r"
exp_continue
}
eof {
catch wait result
}
}
exit [lindex $result 3]
根据 expect 的手册页:
expect [[-opts] pat1 body1] ... [-opts] patn [bodyn]
waits until one of the patterns matches the output of a spawned process, a specified time period has passed, or an end-of-file is seen. If the final body is empty, it may be omitted. Patterns from the most recent
expect_before
command are implicitly used before any other patterns. Patterns from the most recentexpect_after
command are implicitly used after any other patterns.If the arguments to the entire expect statement require more than one line, all the arguments may be braced into one so as to avoid terminating each line with a backslash. In this one case, the usual Tcl substitutions will occur despite the braces.