期待

编程入门 行业动态 更新时间:2024-10-27 09:40:59
期待 - 期待互动(Expect - Expecting while interacting)

我想在“交互”模式下“听”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 fuzz

The "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 }

更多推荐

本文发布于:2023-07-14 23:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1108374.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:期待

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!