如何使用 expect 将程序的 stdout 不断重定向到生成的进程中?

How do I redirect stdout from a program continuously into a spawned process using expect?

需要打开 telnet,发送一些命令,然后从 pocketsphinx 发送标准输出。

目前expect会等到程序执行完毕,然后将所有内容输出到telnet进程。 我需要 pocketsphix 来持续提供生成的 telnet 进程。

这是我目前拥有的:

#!/usr/bin/expect -d
set send_human {.1 .3 1 .05 2}
spawn telnet 192.168.1.104 23
expect “*”
send "\x01"; send "2\r"
expect “:”
send -h "hello world\r"
send -h "goodbye world\r"
send -h "Test Test Test\r"
send -- [exec pocketsphinx_continuous -infile speech.wav 2> /dev/null ]\n

您可以使用 expect 命令 interact 将两个生成的进程连接在一起。

By default, interact expects the user to be writing stdin and reading stdout of the Expect process itself. The -u flag (for "user") makes interact look for the user as the process named by its argument (which must be a spawned id).

This allows two unrelated processes to be joined together without using an explicit loop. To aid in debugging, Expect diagnostics always go to stderr (or stdout for certain logging and debugging information). For the same reason, the interpreter command will read interactively from stdin.

例如

set send_human {.1 .3 1 .05 2}
spawn telnet 192.168.1.104 23
expect “*”
send "\x01"; send "2\r"
expect “:”
send -h "hello world\r"
send -h "goodbye world\r"
send -h "Test Test Test\r"

set sid_telnet $spawn_id
spawn pocketsphinx_continuous -infile speech.wav 2> /dev/null 

interact -u $sid_telnet