expect script: string with square braces doesn'

2019-08-11 06:39发布

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

标签: expect
1条回答
Evening l夕情丶
2楼-- · 2019-08-11 07:09

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).

查看更多
登录 后发表回答