如何检查期望块内命令的成功或失败

时间:2019-01-31 18:32:09

标签: bash shell expect

我有一个bash代码,该代码使用之间的期望通过终端从终端读取。代码如下所示:

expect <{cat <<'EOD'
spawn command_whose_success_or_failure_i_want_to_know
interact
EOD
)

问题是在Expect块中查找“ command_whose_success_or_failure_i_want_to_know”的成功或失败。

当我正在做的时候:

if [ $? -eq 0 ]; then
blah

它实际上是在检查预期的成功,而不是我要检查的内部命令。 我该怎么办? 谢谢

1 个答案:

答案 0 :(得分:0)

您将执行此操作(未经测试):

expect <<'END_EXPECT'
    # if your command takes > 10 seconds to complete, uncomment the next line:
    # set timeout $some_number_of_seconds_or_negative_one

    spawn command_whose_success_or_failure_i_want_to_know
    expect eof

    set result [wait]
    if {[lindex $result 2] == 0} {
        exit [lindex $result 3] ;# exit expect with the command's exit status
    } else {
        error "an operating system error occurred, errno=[lindex $result 3]"
    }
END_EXPECT
exit_status=$?

echo "the command exited with: $exit_status"