3

Consider an expect script that spawns a command and waits for a sequence of events. We use this to test software. I'm trying to always get a failure return value from the script when there is a timeout.

spawn some-command

set timeout 10

expect "First message"
expect "Other message"

This script will always return 0 (success) even when the timeout hits. The only way to handle timeouts is to convert every expect statement with:

expect {
    timeout { exit 1 }
    eof     { exit 1 }
    "First message"
}

But this quickly becomes unwieldy. Are there any simplifications? Is there an expect option that makes early timeout / eof fatal errors?

2 Answers2

3

Commenting on meuh's answer, expect_before takes the same arguments as expect, so you can write it like this:

expect_before {
    timeout { puts "timeout"; exit 2 }
    eof     { puts "eof";     exit 1 }
}
# ...
expect "First message"
glenn jackman
  • 4,320
  • 16
  • 19
2

You can setup an expect_before command. This will be run every time you use expect later in the script. Eg

proc abort { } { send_user "Timeout!" ; exit 2 }
set timeout 5
spawn ssh myhost
expect_before timeout abort
expect  "assword: "
send "abc\r"
expect  "$ "
send "sleep 99\r"
expect  "$ "
meuh
  • 1,288
  • 9
  • 11