expect script: string with square braces doesn't match

ε祈祈猫儿з 提交于 2020-01-05 08:47:33

问题


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

It works as bellow:

$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

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

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

回答1:


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\\\] "

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



来源:https://stackoverflow.com/questions/31601455/expect-script-string-with-square-braces-doesnt-match

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!