2

I was testing a expect script and found two different behaviors when using set timeout vs. expect -timeout. Here is a simple example

response.sh:

#!/usr/bin/bash
sleep 2
echo hello

test1.sh:

#!/usr/bin/expect

spawn ./response.sh set timeout -1 expect { timeout { send_user "timeout\n" } "hello" { send_user "received hello\n" } }

Output (works as expected):

spawn ./response.sh
hello
received hello

however when I use the -timeout flag in expect I never get a response back no matter what value I use for timeout:

#!/usr/bin/expect

spawn ./response.sh expect -timeout 1 { timeout { send_user "timeout\n" } "hello" { send_user "received hello\n" } }

Output:

spawn ./response.sh

I am expecting timeout to be also printed in stdout. Am I missing anything?

1 Answers1

3

The problem is explained fully in the Stack Overflow post
Why doesn't this expect handle timeout or eof.

In short, the -timeout flag should be used prior to the Expect's pattern, not on the actions. With this flag you have signaled to override the global timeout variable, but that's all.

Your code should be re-arranged with -timeout 1 following the braces, rather than before.

See the linked post for an example and fuller explanations.

harrymc
  • 498,455