我使用Expect运行测试脚本。
通过退出代码测试返回成功/失败。但期望返回等效的退出代码。
如何使期望返回正确的退出状态?
我的测试是使用psql(postgresql命令处理器)运行的sql脚本。
由于psql不允许将数据库密码指定为命令行参数,因此需要脚本来执行。
因此,我的期望脚本如下所示:
spawn $SPAWN_CMD
expect {
-re "Enter password for new role:" {
send "$PWPROMPT\n"
exp_continue
} -re "Enter it again:" {
send "$PWPROMPT\n"
exp_continue
} -re "Password(.*)" {
send "$PASSWORD\n"
exp_continue
} -re "Password(.*):" {
send "$PASSWORD\n"
exp_continue
} eof
}
最佳答案
您已经在循环结束时等待eof
了,您只需要使用wait
和catch
结果即可:
spawn true
expect eof
catch wait result
exit [lindex $result 3]
以0退出。
spawn false
expect eof
catch wait result
exit [lindex $result 3]
以1.退出。