我正在尝试使用disque从一个名为disque的面向消息的中间件应用程序中自动调用一些调用,而且我遇到了一些困难......
每次我希望客户端运行命令时,我必须期待它提供的提示并发送命令,但是当我尝试不止一次时,我遇到了问题。
以下是最基本的非功能形式的expect脚本:
set timeout 20
spawn "./disque"
set timeout 10
expect "127.0.0.1:7711> " {send "ADDJOB scriptqueue body 0\r"}
set timeout 10
expect "127.0.0.1:7711> " {send "GETJOB FROM scriptqueue\r"}
但是,当我运行上面的脚本时,只读取第一个提示并发送第一条消息。 GETJOB命令永远不会运行。
以下是我得到的输出:
spawn ./disque
127.0.0.1:7711> ADDJOB scriptqueue body 0
另外作为参考,当我在提示时手动输入这两个字符串时,这就是disque如何正常工作:
127.0.0.1:7711> ADDJOB scriptqueue body 0
DI530404a85743c0afe1353ae1943df8d86c9f561005a0SQ
127.0.0.1:7711> GETJOB FROM scriptqueue
1) 1) "scriptqueue"
2) "DI530404a806ff88886c366ba3566d7e36ea81f89605a0SQ"
3) "body"
127.0.0.1:7711>
更新:
我只是使用-d为调试模式运行脚本并获得此输出:
expect version 5.45
argv[0] = expect argv[1] = -d argv[2] = cng4.sh
set argc 0
set argv0 "cng4.sh"
set argv ""
executing commands from command file cng4.sh
spawn ./disque
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {17657}
expect: does "" (spawn_id exp4) match glob pattern "*>"? no
127.0.0.1:7711>
expect: does "127.0.0.1:7711> " (spawn_id exp4) match glob pattern "*>"? yes
expect: set expect_out(0,string) "127.0.0.1:7711>"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "127.0.0.1:7711>"
send: sending "ADDJOB scriptqueue body 0\r" to { exp4 }
expect: does " " (spawn_id exp4) match glob pattern "*>"? no
127.0.0.1:7711> ADDJOB scriptqueue body 0
DI530404a8efd0d77b5bb62a7568510f7b6a29a13405a0SQ
127.0.0.1:7711>
expect: does " \r\u001b[0K127.0.0.1:7711> A\r\u001b[17C\r\u001b[0K127.0.0.1:7711> AD\r\u001b[18C\r\u001b[0K127.0.0.1:7711> ADD\r\u001b[19C\r\u001b[0K127.0.0.1:7711> ADDJ\r\u001b[20C\r\u001b[0K127.0.0.1:7711> ADDJO\r\u001b[21C\r\u001b[0K127.0.0.1:7711> ADDJOB\r\u001b[22C\r\u001b[0K127.0.0.1:7711> ADDJOB \r\u001b[23C\r\u001b[0K127.0.0.1:7711> ADDJOB s\r\u001b[24C\r\u001b[0K127.0.0.1:7711> ADDJOB sc\r\u001b[25C\r\u001b[0K127.0.0.1:7711> ADDJOB scr\r\u001b[26C\r\u001b[0K127.0.0.1:7711> ADDJOB scri\r\u001b[27C\r\u001b[0K127.0.0.1:7711> ADDJOB scrip\r\u001b[28C\r\u001b[0K127.0.0.1:7711> ADDJOB script\r\u001b[29C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptq\r\u001b[30C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqu\r\u001b[31C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptque\r\u001b[32C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueu\r\u001b[33C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue\r\u001b[34C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue \r\u001b[35C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue b\r\u001b[36C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue bo\r\u001b[37C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue bod\r\u001b[38C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue body\r\u001b[39C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue body \r\u001b[40C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue body 0\r\u001b[41C\r\nDI530404a8efd0d77b5bb62a7568510f7b6a29a13405a0SQ\r\n127.0.0.1:7711> " (spawn_id exp4) match glob pattern "*>"? yes
expect: set expect_out(0,string) " \r\u001b[0K127.0.0.1:7711> A\r\u001b[17C\r\u001b[0K127.0.0.1:7711> AD\r\u001b[18C\r\u001b[0K127.0.0.1:7711> ADD\r\u001b[19C\r\u001b[0K127.0.0.1:7711> ADDJ\r\u001b[20C\r\u001b[0K127.0.0.1:7711> ADDJO\r\u001b[21C\r\u001b[0K127.0.0.1:7711> ADDJOB\r\u001b[22C\r\u001b[0K127.0.0.1:7711> ADDJOB \r\u001b[23C\r\u001b[0K127.0.0.1:7711> ADDJOB s\r\u001b[24C\r\u001b[0K127.0.0.1:7711> ADDJOB sc\r\u001b[25C\r\u001b[0K127.0.0.1:7711> ADDJOB scr\r\u001b[26C\r\u001b[0K127.0.0.1:7711> ADDJOB scri\r\u001b[27C\r\u001b[0K127.0.0.1:7711> ADDJOB scrip\r\u001b[28C\r\u001b[0K127.0.0.1:7711> ADDJOB script\r\u001b[29C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptq\r\u001b[30C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqu\r\u001b[31C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptque\r\u001b[32C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueu\r\u001b[33C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue\r\u001b[34C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue \r\u001b[35C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue b\r\u001b[36C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue bo\r\u001b[37C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue bod\r\u001b[38C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue body\r\u001b[39C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue body \r\u001b[40C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue body 0\r\u001b[41C\r\nDI530404a8efd0d77b5bb62a7568510f7b6a29a13405a0SQ\r\n127.0.0.1:7711>"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) " \r\u001b[0K127.0.0.1:7711> A\r\u001b[17C\r\u001b[0K127.0.0.1:7711> AD\r\u001b[18C\r\u001b[0K127.0.0.1:7711> ADD\r\u001b[19C\r\u001b[0K127.0.0.1:7711> ADDJ\r\u001b[20C\r\u001b[0K127.0.0.1:7711> ADDJO\r\u001b[21C\r\u001b[0K127.0.0.1:7711> ADDJOB\r\u001b[22C\r\u001b[0K127.0.0.1:7711> ADDJOB \r\u001b[23C\r\u001b[0K127.0.0.1:7711> ADDJOB s\r\u001b[24C\r\u001b[0K127.0.0.1:7711> ADDJOB sc\r\u001b[25C\r\u001b[0K127.0.0.1:7711> ADDJOB scr\r\u001b[26C\r\u001b[0K127.0.0.1:7711> ADDJOB scri\r\u001b[27C\r\u001b[0K127.0.0.1:7711> ADDJOB scrip\r\u001b[28C\r\u001b[0K127.0.0.1:7711> ADDJOB script\r\u001b[29C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptq\r\u001b[30C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqu\r\u001b[31C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptque\r\u001b[32C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueu\r\u001b[33C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue\r\u001b[34C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue \r\u001b[35C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue b\r\u001b[36C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue bo\r\u001b[37C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue bod\r\u001b[38C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue body\r\u001b[39C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue body \r\u001b[40C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue body 0\r\u001b[41C\r\nDI530404a8efd0d77b5bb62a7568510f7b6a29a13405a0SQ\r\n127.0.0.1:7711>"
send: sending "GETJOB FROM scriptqueue\r" to { exp4 }
看起来原因是它正在从disque应用程序本身中获取其他一些输出......我仍然不确定如何解决这个问题。
感谢。
答案 0 :(得分:1)
从调试日志中可以看出,GETJOB
确实已发送到应用程序。问题是你没有等GETJOB
完成。想想当你手动与应用程序交互时,你怎么知道GETJOB
已经完成了?你等下一个提示,对吧?所以告诉expect
做同样的事情。也就是说,告诉expect
在它继续前进(或退出)之前等待下一个提示。
expect "127.0.0.1:7711> " {send "GETJOB FROM scriptqueue\r"}
expect "127.0.0.1:7711> "