Expect 预期脚本:带方括号的字符串不';不匹配

Expect 预期脚本:带方括号的字符串不';不匹配,expect,Expect,我是一个完全不懂指挥的人。 假设有一个名为“a.rb”的脚本文件,它是用ruby编写的: STDOUT << 'Overwrite /opt/rails/rails_app/Gemfile? (enter "h" for help) [Ynaqdh] ' s = STDIN.gets STDOUT << s 如果我想自动化用户的输入,使用expect命令似乎很好 因此,我尝试创建一个脚本文件(a.expect): 但是这个脚本不起作用,我也不知道为什么。 这是我的问题

我是一个完全不懂指挥的人。 假设有一个名为“a.rb”的脚本文件,它是用ruby编写的:

STDOUT << 'Overwrite /opt/rails/rails_app/Gemfile? (enter "h" for help) [Ynaqdh] '
s = STDIN.gets
STDOUT << s
如果我想自动化用户的输入,使用expect命令似乎很好

因此,我尝试创建一个脚本文件(a.expect):

但是这个脚本不起作用,我也不知道为什么。 这是我的问题

$ expect -f a.expect
spawn ruby a.rb
Overwrite /opt/rails/rails_app/Gemfile? (enter "h" for help) [Ynaqdh] 
$  # <= expect script has finished because of (maybe) timeout?!
$expect-f a.expect
繁殖ruby a.rb
是否覆盖/opt/rails/rails\u app/Gemfile?(输入“h”以获取帮助)[Ynaqdh]

$#这些字符串匹配,但它们不相互关联。你需要使用更多的逃犯

expect "Overwrite /opt/rails/rails_app/Gemfile\? \(enter \"h\" for help\) \\\[Ynaqdh\\\] "

这是因为
[abcd]
匹配
a
b
c
d
--一个字符--而不是
[abcd]
(六个字符的字符串)。

除此之外,它是一个TCL扩展名。它与bash无关……另一方面,我建议不要在这个用例中使用
expect
,这太过分了。为什么不使用
yes y | ruby a.rb
?…但是,如果要使用
expect
,请使用
expect-d
进行调试。我不知道yes命令。太好了……只有当真正的
a.rb
没有办法在非交互模式下运行它时,它才不需要在stdin上输入。大多数工具都是这样做的,如果没有,你应该考虑提交补丁。我改变了反斜杠的数量(<代码>期望)覆盖/ opt/Rails /Rails SpApp/GEMFILE \\(输入\“H”以帮助\)\\[YNAQDH\\\ ] /<代码>。然而,它并没有按预期的那样工作。但不管怎样,“是的”命令对我来说是个很好的回答。它立刻解决了我的问题。我不坚持使用expect。它确实会将
y
发送回应用程序。如果您没有看到应用程序回显
y
,我建议不要立即退出。例如,您可以在末尾放置一个
expect y
$ expect -f a.expect
spawn ruby a.rb
Overwrite /opt/rails/rails_app/Gemfile? (enter "h" for help) [Ynaqdh] 
$  # <= expect script has finished because of (maybe) timeout?!
expect "Overwrite /opt/rails/rails_app/Gemfile\? \(enter \"h\" for help\) \\\[Ynaqdh\\\] "