期望脚本:带有大括号的字符串不匹配 [英] expect script: string with square braces doesn't match

查看:92
本文介绍了期望脚本:带有大括号的字符串不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绝对是新来的命令. 假设有一个名为"a.rb"的脚本文件,它是用ruby编写的:

I'm absolutely new to expect command. Suppose that there is a script file named 'a.rb', which is written in ruby:

STDOUT << 'Overwrite /opt/rails/rails_app/Gemfile? (enter "h" for help) [Ynaqdh] '
s = STDIN.gets
STDOUT << s

它像下面一样工作:

$ruby a.rb
Overwrite /opt/rails/rails_app/Gemfile? (enter "h" for help) [Ynaqdh] #wait user's input
y  # show the user's input and exit

如果我想自动化用户的输入,使用Expect命令似乎很好.

It seems good to use expect command if I want to automate the user's input.

所以我试图制作一个脚本文件(a.expect):

So I tried to make a script file (a.expect):

spawn ruby a.rb
expect "Overwrite /opt/rails/rails_app/Gemfile\? \(enter \"h\" for help\) \[Ynaqdh\] "
sleep 3
send "y\r"

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

But this script doesn't work and I don't know why. That is my question.

$ 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?!

推荐答案

这些字符串匹配,但是它们不会相互干扰.您需要使用更多的转义符.

These strings match, but they don't glob to each other. You need to use more escapes.

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

这是因为[abcd]匹配abcd-一个字符-不是[abcd](六个字符的字符串).

This is because [abcd] matches a, b, c or d -- one character -- not [abcd] (a six-character string).

这篇关于期望脚本:带有大括号的字符串不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆