提问人:Alexey Semenyuk 提问时间:9/6/2023 最后编辑:Alexey Semenyuk 更新时间:9/6/2023 访问量:26
如何从 expect 脚本关闭 stdin
How to close stdin from expect script
问:
问题是如何从期望脚本中输入命令并通知它有关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 1
send -- "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
答: 暂无答案
评论
sleep 1
send -- "Hello\n"
sleep 1
sleep 1