23

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
}
seas
  • 1,482
  • 2
  • 18
  • 29

1 Answers1

34

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.

Douglas Leeder
  • 52,368
  • 9
  • 94
  • 137
  • 1
    Hmm.. `[21:43:30] ~> expect <<< "spawn true; expect eof; catch wait result; exit [lindex $result 3]"; echo $?` outputs `3` for me – sasha.sochka Nov 17 '14 at 19:48
  • 3
    @sasha.sochka If you type it like that $result gets interpreted by the shell rather than expect. Put the input in single quotes, or escape the $. – Douglas Leeder Nov 18 '14 at 13:14