开发者

How to return spawned process exit code in Expect script?

开发者 https://www.devze.com 2023-01-08 10:17 出处:网络
I use expect for running test scripts. Tests return success/failure through exit code. But expect return equivalent exit code.

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?

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\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
}


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]

Exits with 0.

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

Exits with 1.

0

精彩评论

暂无评论...
验证码 换一张
取 消