Expect脚本中向cat发送EOF无效的解决方法(Cygwin环境)
如何在Expect脚本中向
cat命令输入内容并发送EOF信号? 我需要在Expect脚本里向cat命令输入内容,之后通知它到达EOF,但尝试用send \004发送EOF控制序列时完全无效,导致脚本超时。
初始脚本
#!/usr/bin/expect spawn bash -c "echo READY; foo=\$(cat); echo DONE" expect "READY\r\n" send -- "Hello\n" # 此操作无效 send \004 # 此处会超时 expect "DONE\r\n"
调试输出(执行expect -d test.exp)
$ 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环境特有的,只需在send -- "Hello\n"之后添加sleep 1即可解决:
#!/usr/bin/expect spawn bash -c "echo READY; foo=\$(cat); echo DONE" expect "READY\r\n" send -- "Hello\n" sleep 1 # 若无前置sleep 1,此操作在Cygwin中无效 send \004 expect "DONE\r\n" exit
内容的提问来源于stack exchange,提问作者Alexey Semenyuk
相关产品推荐
相关产品推荐

