Brad.Smith
Brad.Smith

Reputation: 1111

Return exit status code from inline Expect command

How do you make Expect to return the exit status codes rather than a 0 or 1 when executing expect -c.

For example, after executing the following line I expect to have "17" returned, but instead I get "0" signalling that the command successfully ran.

expect -c "spawn echo \"foo\"; expect { -re \"foo\" { exit 17 } }"; echo $?

Upvotes: 3

Views: 1453

Answers (1)

Johannes Kuhn
Johannes Kuhn

Reputation: 15173

After a little testing I found that this:

expect -c "spawn echo foo; expect \"\n-re . {puts OK}\""

which is equivalent to the following Expect script:

spawn echo foo; expect {
-re . {puts OK}}

works as expected. But if you remove the \n it will fail. So Expect uses only the multiline syntax when the first character of the argument is a new line. Otherwise it tries to use that for matching, in your case it tries to match -re "foo" { exit 17 }. If you add a exp_internal 1 at the beginning, expect will output this line:

expect: does "foo\r\n" (spawn_id exp6) match glob pattern " -re "foo" { exit 17 } "? no

So simply omit the {} for the expect group.
The correct statement should be:

expect -c "spawn echo \"foo\"; expect -re \"foo\" { exit 17 }"; echo $?

Upvotes: 5

Related Questions