gavenkoa
gavenkoa

Reputation: 48833

"expect" match "send" string, why?

I found that expect command just after send command match data from send command.

Let see, my.sh:

#!/bin/sh
read line
echo ok

my.exp (some code is redundant, I emulate DejaGNU testing framework...):

set passn 0
proc pass {msg} { global passn; incr passn; send_user "PASS: $msg\n" }
set failn 0
proc fail {msg} { global failn; incr failn; send_user "FAIL: $msg\n" }
proc check {} {
    global passn failn;
    send_user "TOTOAL: [expr $passn + $failn], PASSED: $passn, FAIL: $failn\n"
    if {$failn == 0} { exit 0 } { exit 1 }
}

set timeout 1

spawn ./my.sh

send hello\n
expect {
    -ex hello {
        send_user "~$expect_out(0,string)~\n"
        pass hello
    }
    default { fail hello }
}
expect {
    -ex ok { pass ok }
    default { fail ok }
}

check

When run expect my.exp I get:

spawn ./my.sh
hello
~hello~
PASS: hello
ok
PASS: ok
TOTOAL: 2, PASSED: 2, FAIL: 0

I don't understand why hello matched!! Please tell me. I already reread:

Upvotes: 2

Views: 1014

Answers (1)

Anton Kovalenko
Anton Kovalenko

Reputation: 21507

expect works with pseudoterminal devices. They are much like normal terminals: unless you disable echo (using expect's stty command), whatever you send is also visible as your "terminal output" (which is expect's input).

stty_init variable is used by expect to set up newly created pseudoterminals. If you add set stty_init -echo to the beginnig of my.exp, the test will begin to fail.

Upvotes: 4

Related Questions