使用 Tcl Expect 生成 Ruby IRB 悲惨地以破裂的管道结束!
我只是想打开一个 irb 窗口并输入命令。这曾经有效,但现在不知何故被破坏了:
package require Expect
set exp::winnt_debug 1
set errorInfo
set SPAWN_ID ""
set EXPECT_TIMEOUT 20
set PROMPT {irb.*[*">] }
set RUBY_HOME "C:/ruby/"
exp_spawn [file join $RUBY_HOME "bin" "ruby.exe"]\
[file join $RUBY_HOME "bin" "irb"] "--noinspect"
set SPAWN_ID $spawn_id
puts $spawn_id
expect {
-i $SPAWN_ID\
-timeout $EXPECT_TIMEOUT\
-re $PROMPT {
set retVal 1
puts "retVal 1"
}
timeout {
set retVal 0
puts "retVal 0"
}
}
match_max -i $SPAWN_ID 10000
send -i $SPAWN_ID "Hello World\r"
我使用的是WindowsXP SP3, 红宝石 1.8.7, Tcl 8.5.10, 预计 5.43.2 等。
谢谢,汤姆
irb(main):001:0> retVal 1
write(spawn_id=]: broken pipe
while executing
"send -i $SPAWN_ID "Hello World\r""
(file "TomSpawnRuby.tcl" line 37)
I am simply trying to open an irb window and enter commands. This used to work but somehow now it is broken:
package require Expect
set exp::winnt_debug 1
set errorInfo
set SPAWN_ID ""
set EXPECT_TIMEOUT 20
set PROMPT {irb.*[*">] }
set RUBY_HOME "C:/ruby/"
exp_spawn [file join $RUBY_HOME "bin" "ruby.exe"]\
[file join $RUBY_HOME "bin" "irb"] "--noinspect"
set SPAWN_ID $spawn_id
puts $spawn_id
expect {
-i $SPAWN_ID\
-timeout $EXPECT_TIMEOUT\
-re $PROMPT {
set retVal 1
puts "retVal 1"
}
timeout {
set retVal 0
puts "retVal 0"
}
}
match_max -i $SPAWN_ID 10000
send -i $SPAWN_ID "Hello World\r"
I am using WindowsXP SP3,
Ruby 1.8.7,
Tcl 8.5.10,
Expect 5.43.2, etc.
Thanks, Tom
irb(main):001:0> retVal 1
write(spawn_id=]: broken pipe
while executing
"send -i $SPAWN_ID "Hello World\r""
(file "TomSpawnRuby.tcl" line 37)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在寻找匹配的内容时,Expect 搜索的空间可能包含换行符,因此在使用量词时采取措施确保不会无意中越过行非常重要。例如,我怀疑以下提示更有可能起作用:
在解析提示的不同部分时,您还可以使用一组更受限制的内容:
编写一个与您想要的完全匹配的正则表达式可能需要相当多的时间的努力;这确实是一门艺术,但是阅读 Tcl RE 语法的定义 可以提供很多帮助。 (在过去 10 年里没有太大变化。)
另一件需要检查的事情是 Windows 上的 Ruby 是否会因为文件名中的正斜杠而卡住。如果是这样,您需要使用
file nativename
进行转换:(在这种情况下,我不会使用
file join
。)The space searched by Expect when looking for things to match can include newlines, so it is important when using quantifiers to take steps to ensure that you don't go across lines inadvertently. For example, I suspect that the following prompt is more likely to work:
You could also use a more restricted set of things when parsing the varying part of the prompt:
Writing a regular expression that will match exactly what you want can take quite a bit of effort; it's a bit of an art, really, but reading the definition of Tcl's RE syntax can help a lot. (It's not changed much over the past 10 years.)
Another thing to check for is whether Ruby on Windows chokes on forward slashes in filenames. If it does, you'll need to use
file nativename
to convert:(I wouldn't bother with using
file join
in this situation.)通过查看语法突出显示,我建议这里的双引号
set PROMPT {irb.*[*">] }
需要转义。\"
我知道但与红宝石无关,所以如果我错了请纠正我From looking at the syntax highlighting i would suggest that the double quote here
set PROMPT {irb.*[*">] }
needs to be escaped.\"
I know nothing of ruby though so correct me if i'm wrong