使用 Tcl Expect 生成 Ruby IRB 悲惨地以破裂的管道结束!

发布于 2024-11-26 15:05:42 字数 929 浏览 2 评论 0原文

我只是想打开一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与他有关 2024-12-03 15:05:43

在寻找匹配的内容时,Expect 搜索的空间可能包含换行符,因此在使用量词时采取措施确保不会无意中越过行非常重要。例如,我怀疑以下提示更有可能起作用:

set PROMPT {(?n)^irb.*[*">] }

在解析提示的不同部分时,您还可以使用一组更受限制的内容:

set PROMPT {(?n)^irb[^\s*">]*[*">] }

编写一个与您想要的完全匹配的正则表达式可能需要相当多的时间的努力;这确实是一门艺术,但是阅读 Tcl RE 语法的定义 可以提供很多帮助。 (在过去 10 年里没有太大变化。)


另一件需要检查的事情是 Windows 上的 Ruby 是否会因为文件名中的正斜杠而卡住。如果是这样,您需要使用 file nativename 进行转换:(

exp_spawn $RUBY_HOME/bin/ruby.exe [file nativename $RUBY_HOME/bin/irb] --noinspect

在这种情况下,我不会使用 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:

set PROMPT {(?n)^irb.*[*">] }

You could also use a more restricted set of things when parsing the varying part of the prompt:

set PROMPT {(?n)^irb[^\s*">]*[*">] }

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:

exp_spawn $RUBY_HOME/bin/ruby.exe [file nativename $RUBY_HOME/bin/irb] --noinspect

(I wouldn't bother with using file join in this situation.)

娜些时光,永不杰束 2024-12-03 15:05:43

通过查看语法突出显示,我建议这里的双引号 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文