如何在 Expect 脚本中返回生成的进程退出代码? [英] How to return spawned process exit code in Expect script?
问题描述
我使用 expect 来运行测试脚本.测试通过退出代码返回成功/失败.但期望返回等效的退出代码.如何让expect返回正确的退出状态?
I use expect for running test scripts. Tests return success/failure through exit code. But expect return equivalent exit code. How to make expect return proper exit status?
我的测试是使用 psql(postgresql 命令处理器)运行的 sql 脚本.由于 psql 不允许将数据库密码指定为命令行参数,期望脚本这样做.
My tests are sql scripts run with psql (postgresql command processor). Since psql doesn't allow to specify database password as a command line parameter, expect scripts do that.
所以,我的期望脚本如下:
So, my expect script looks like:
spawn $SPAWN_CMD
expect {
-re "Enter password for new role:" {
send "$PWPROMPT
"
exp_continue
} -re "Enter it again:" {
send "$PWPROMPT
"
exp_continue
} -re "Password(.*)" {
send "$PASSWORD
"
exp_continue
} -re "Password(.*):" {
send "$PASSWORD
"
exp_continue
} eof
}
推荐答案
你已经在等待循环结束时的 eof
,你只需要使用 wait
和 catch
结果:
You're already waiting for the eof
at the end of your loop, you just need to use wait
and catch
the result:
spawn true
expect eof
catch wait result
exit [lindex $result 3]
以 0 退出.
spawn false
expect eof
catch wait result
exit [lindex $result 3]
以 1 退出.
这篇关于如何在 Expect 脚本中返回生成的进程退出代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!