2010-07-21 65 views
22

我使用expect来运行测试脚本。 测试通过退出代码返回成功/失败。但期望返回等价退出代码。 如何让期望返回正确的退出状态?如何在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 
} 

回答

27

你已经在你的循环结束等待eof,你只需要使用waitcatch结果:

spawn true 
expect eof 
catch wait result 
exit [lindex $result 3] 

退出以0

spawn false 
expect eof 
catch wait result 
exit [lindex $result 3] 

1退出。

+0

嗯.. '[21:43:30]〜>期望<<<“产卵真实;期待eof;赶上等待结果;退出[lindex $ result 3]“; echo $?'输出'3'对我来说 – 2014-11-17 19:48:33

+2

@ sasha.sochka如果你输入它就像$ result被shell解释而不是期望的一样,把输入放在单引号或者escape $。 – 2014-11-18 13:14:52

+0

哦,我的错,明白了,谢谢 – 2014-11-18 13:15:44

相关问题