2

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:

gavenkoa
  • 45,285
  • 19
  • 251
  • 303

1 Answers1

4

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.

Anton Kovalenko
  • 20,999
  • 2
  • 37
  • 69
  • Thanks for answer. I don't hope that question about **Expect** can be answered in SO! One think that I don't understand why my code fail with **stty -echo**, which I try when debug issue, but with **stty_init -echo** work... Just a minute I going to read **man expect**... – gavenkoa Feb 04 '13 at 07:51
  • Also with **set stty_init {-echo raw}** I fix issue with **\r**. I use **Expect** to test my line oriented protocol and it is annoying instead of **\n** use **\r\n** in match patterns... – gavenkoa Feb 04 '13 at 08:06