如何将退出状态从Expect传播到其父Bash脚本?

时间:2012-12-22 00:31:59

标签: linux bash shell expect exit-code

我有一个bash脚本,exec.sh就像

some command
expect test.exp
continue other command
在test.exp文件中的

,我有一个代码片段:

while {[gets $cmds command]>=0} {
  send "$command\r"
  expect {
    "*OK*" {puts $vout $command}
    "*fail*" {puts $iout $command}
    "*blocked*" { what should I put here????}
    }  
  }

所以我想把一些东西放在大括号中,以便执行退出test.exp并发出bash脚本exec.sh的信号,所以exec.sh也会退出 我的想法是设置一个外部变量,然后在exec.sh中使用“if”判断语句

有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:2)

从期望中传递退出状态

Tcl(因此Expect)有一个exit命令,它接受一个参数。参数是流程的退出状态。您可以为退出状态指定含义,并从shell脚本测试退出状态。例如,使用 /usr/include/sysexits.h 中的值,您可以写:

expect {
  "blocked" { exit 69 }
}

然后在脚本中测试该值。

在Shell中的退出状态分支

最后一个进程的退出状态存储在$?变量。测试这种方法的一种方法是使用case语句,并相应地进行分支。例如:

expect test.exp
case $? in
  69)
    # Handle the exit status, and then propagate the same exit status
    # from the shell script.
    echo 'service unavailable' > /dev/stderr
    exit 69
    ;;
esac
相关问题