我正在expect脚本中运行一个命令,如果命令返回值不为0,我想退出expect脚本。我将变量ret设置为“$?”像这样,这是行不通的:
send "./$SCRIPT_NAME >> out.txt &\r"
expect "~#"
set ret $?
if { $ret != 0} {
exit $ret
}
但我得到这个输出:
expected integer but got "$?"
while executing
"exit $ret"
invoked from within
"if { $ret != 0} {
exit $ret
}"
$ 相当于什么?期待吗?
这有点令人痛苦。
首先,由于您需要获取退出状态,因此您不想在后台启动脚本。无论如何,您都必须使用 shell 的
wait
命令来获取退出状态。
接下来,您必须要求 shell 打印退出状态并使用
expect
命令捕获该状态。请注意,对于行结尾,期望返回 \r\n
。
send "./$SCRIPT_NAME >> out.txt; echo $?\r"
expect -re {(\d+)\r\n~#}
# ..........^^^^^
# exit status is captured in the 1st set of parens
set ret $expect_out(1,string)
if { $ret != 0} {
exit $ret
}