期望脚本:带方括号的字符串不匹配
expect script: string with square braces doesn't match
我对命令完全陌生。
假设有一个名为'a.rb'的脚本文件,写在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 命令似乎不错。
所以我试着做了一个脚本文件(a.expect):
spawn ruby a.rb
expect "Overwrite /opt/rails/rails_app/Gemfile\? \(enter \"h\" for help\) \[Ynaqdh\] "
sleep 3
send "y\r"
但是这个脚本不起作用,我不知道为什么。
这是我的问题。
$ 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\\] "
这是因为 [abcd]
匹配 a
、b
、c
或 d
-- 一个字符 -- 而不是 [abcd]
(六个字符的字符串)。
我对命令完全陌生。 假设有一个名为'a.rb'的脚本文件,写在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 命令似乎不错。
所以我试着做了一个脚本文件(a.expect):
spawn ruby a.rb
expect "Overwrite /opt/rails/rails_app/Gemfile\? \(enter \"h\" for help\) \[Ynaqdh\] "
sleep 3
send "y\r"
但是这个脚本不起作用,我不知道为什么。 这是我的问题。
$ 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\\] "
这是因为 [abcd]
匹配 a
、b
、c
或 d
-- 一个字符 -- 而不是 [abcd]
(六个字符的字符串)。