期望:如何将 EOF 发送到生成的进程

expect: how to send an EOF to spawnd process

我有一个程序从标准输入读取并处理它。 (像 "tee /some/file" )

此程序等待 stdin 结束以自行退出。

如果我从 Expect 生成它,在我 send 很多内容之后,如何向程序发送 "EOF"?

Expect中有close命令,但也会发送SIGHUP,无法expect程序输出

Expect 通过使用生成程序在其中运行的虚拟终端来工作(在非 Windows 上)。这意味着您可以通过发送字符序列来模拟按键来做事。特别是 EOF control sequence is done with Ctrl+D,它变成字符 U+000004。终端处理它以将其变成真正的 EOF。

有几种写法,具体取决于您喜欢哪种转义序列,但其中一种可行:

# Hexadecimal-encoded escape
send \x04
# Octal-encoded escape
send [=11=]4
# UNICODE escape (also hexadecimal)
send \u0004
# Generate by a command
send [format "%c" 4]

当 Expect 使用 Tcl 8.6 时,这些都生成相同的字节码,因此请使用您喜欢的任何一个。