我想在“交互”模式下“听”shell输出的字符串。 或者我想以某种方式模拟交互模式,这仍然允许我从shell中监听特定的字符串。
看起来interact只听用户输入(我按的键),而不是shell返回的内容。
如果Expect每次看到一个特定的字符串时执行某些操作,我会如何去做,但是否则让我以交互方式使用shell不受阻碍?
例:
proc yay {} { send_user "Yay\n" } trap { # trap sigwinch and pass it to the child we spawned set rows [stty rows] set cols [stty columns] stty rows $rows columns $cols < $spawn_out(slave,name) } WINCH spawn bash interact { interact -re "HOT" { yay } expect { fuzz yay } }如果我运行这个并输入“HOT”,它会回应“Yay”。 正如所料,它读了我的钥匙。 但是,如果我输入
echo fuzz“期望”条款不会被触发。 此外“回声HOT”也不会触发任何事情。
所以这是可能的还是我错过了一些东西。 也许我需要效仿某种“期望,继续”的interact 。 它只是重要的一切在壳中正常工作..
建议任何人?
I want to "listen" for a string outputted by the shell, while being in "interact" mode. Or i want to emulate interact mode somehow, that still allows me to listen for a specific string from the shell.
It seems interact only listens to the users input (the keys I press) and not what is returned by the shell.
How would I go about having Expect executing something everytime it sees a specific string, but otherwise let me use the shell interactively an unhindered?.
Example:
proc yay {} { send_user "Yay\n" } trap { # trap sigwinch and pass it to the child we spawned set rows [stty rows] set cols [stty columns] stty rows $rows columns $cols < $spawn_out(slave,name) } WINCH spawn bash interact { interact -re "HOT" { yay } expect { fuzz yay } }If i run this and type "HOT" it responds with "Yay". As expected, it read my keys. But if i type
echo fuzzThe "expect" clause doesnt get triggered. Also "echo HOT" wont trigger anything either.
So is this possible or am I missing somthing. Perhaps I'd need to emulate interact in some kind of "expect, continue"-loop. Its just important that everything works normally in the shell..
Suggestions anyone?
最满意答案
您可以使用expect_background命令。 从手册页 :
采用与期望相同的参数,但是它立即返回。 每当新输入到达时都会测试模式。
你可以像这样修改你的初始脚本:
#!/usr/bin/expect proc yay {} { send_user "Yay\n" } trap { # trap sigwinch and pass it to the child we spawned set rows [stty rows] set cols [stty columns] stty rows $rows columns $cols < $spawn_out(slave,name) } WINCH spawn bash expect_background { fuzz yay } interact -re "HOT" { yay }You can use the expect_background command. From the man page:
takes the same arguments as expect, however it returns immediately. Patterns are tested whenever new input arrives.
You can modify your initial script like this:
#!/usr/bin/expect proc yay {} { send_user "Yay\n" } trap { # trap sigwinch and pass it to the child we spawned set rows [stty rows] set cols [stty columns] stty rows $rows columns $cols < $spawn_out(slave,name) } WINCH spawn bash expect_background { fuzz yay } interact -re "HOT" { yay }更多推荐
发布评论