如何从 expect 脚本关闭 stdin

How to close stdin from expect script

提问人:Alexey Semenyuk 提问时间:9/6/2023 最后编辑:Alexey Semenyuk 更新时间:9/6/2023 访问量:26

问:

问题是如何从期望脚本中输入命令并通知它有关EOF的信息。cat

脚本:

#!/usr/bin/expect

spawn bash -c "echo READY; foo=\$(cat); echo DONE"
expect "READY\r\n"
send -- "Hello\n"

# This doesn't work
send \004

# This timesout
expect "DONE\r\n"

send \004什么也不做。显然,没有什么可以将此控制序列转换为 EOF。如何修复/解决它?

使用以下命令运行脚本:expect -d

$ expect -d test.exp
expect version 5.45
argv[0] = expect  argv[1] = -d  argv[2] = test.exp
set argc 0
set argv0 "test.exp"
set argv ""
executing commands from command file test.exp
spawn bash -c echo READY; foo=$(cat); echo DONE
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {36656}

expect: does "" (spawn_id exp4) match glob pattern "READY\r\n"? no
READY

expect: does "READY\r\n" (spawn_id exp4) match glob pattern "READY\r\n"? yes
expect: set expect_out(0,string) "READY\r\n"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "READY\r\n"
send: sending "Hello\n" to { exp4 }
send: sending "\u0004" to { exp4 }

expect: does "" (spawn_id exp4) match glob pattern "DONE\r\n"? no
Hello

expect: does "Hello\r\n" (spawn_id exp4) match glob pattern "DONE\r\n"? no
expect: timed out

更新该问题是特定于 cygwin 的。它通过添加后得到解决:sleep 1send -- "Hello\n"

#!/usr/bin/expect

spawn bash -c "echo READY; foo=\$(cat); echo DONE"
expect "READY\r\n"
send -- "Hello\n"
sleep 1

# This doesn't work on cygwin without prior "sleep 1"
send \004
expect "DONE\r\n"
exit
Cygwin 期待

评论

0赞 sexpect - Expect for Shells 9/6/2023
您的脚本在 macOS 和 Linux 上对我有用。尝试在 之后添加。sleep 1send -- "Hello\n"
0赞 Alexey Semenyuk 9/6/2023
感谢您的尝试!它对我不起作用。可能问题出在pty配置上。
1赞 sexpect - Expect for Shells 9/6/2023
在Cygwin中重现了这个问题,添加对我有用。sleep 1
0赞 Alexey Semenyuk 9/6/2023
我确认添加解决了问题。非常感谢!但现在它提出了另一个问题:为什么cygwin需要它?sleep 1
0赞 sexpect - Expect for Shells 9/6/2023
不确定“问题”是在 Cygwin 终端还是 Cygwin Bash/shell 中。也不知道Cygwin终端在多大程度上符合TTY“标准”。我不会费心去发现它。:)

答: 暂无答案