如何从expect中获取输出

发布于 2024-09-04 23:01:32 字数 834 浏览 3 评论 0原文

我编写了一个脚本来生成 bc 命令

package require Expect

proc bc {eq} {
    spawn e:/GnuWin32/bc/bin/bc
    send "$eq\r"
    expect -re "(.*)\r"
    return "$expect_out(0,string)"
}

set foo "9487294387234/sqrt(394872394879847293847)"

puts "the valule [bc $foo]"

如何从中获取输出。当我运行这个时,我得到这样的输出:

bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
9487294387234/sqrt(394872394879847293847)
477
can't read "expect_out(0,string)": no such element in array
    while executing
"return "The values is $expect_out(0,string)""
    (procedure "bc" line 6)
    invoked from within
"bc $foo"
    invoked from within
"puts "the valule [bc $foo]""
    (file "bc.tcl" line 21)

如何解决这个问题。

i wrote a script for spawing the bc command

package require Expect

proc bc {eq} {
    spawn e:/GnuWin32/bc/bin/bc
    send "$eq\r"
    expect -re "(.*)\r"
    return "$expect_out(0,string)"
}

set foo "9487294387234/sqrt(394872394879847293847)"

puts "the valule [bc $foo]"

how to get the output from this. When i am running this one i get ouput like this

bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
9487294387234/sqrt(394872394879847293847)
477
can't read "expect_out(0,string)": no such element in array
    while executing
"return "The values is $expect_out(0,string)""
    (procedure "bc" line 6)
    invoked from within
"bc $foo"
    invoked from within
"puts "the valule [bc $foo]""
    (file "bc.tcl" line 21)

how to resolve this one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

你是我的挚爱i 2024-09-11 23:01:32

问题是它与您的期望不符。 (提示 Jamie Zawinski 的引言。)

试试这个:

package require Expect
proc bc eq {
    spawn bc; # It's on my path...
    send $eq\r
    set timeout 1; # 1 second
    expect timeout {} -re {[^\r\n]+} {
        set lastline $expect_out(0,string)
        exp_continue
    }
    close
    return $lastline
}
puts >>[bc 9487294387234/sqrt(394872394879847293847)]<<

我得到这个输出......

spawn bc
9487294387234/sqrt(394872394879847293847)
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
9487294387234/sqrt(394872394879847293847)
477
>>477<<

The problem is that it's not matching what you expect. (Cue the Jamie Zawinski quote.)

Try this:

package require Expect
proc bc eq {
    spawn bc; # It's on my path...
    send $eq\r
    set timeout 1; # 1 second
    expect timeout {} -re {[^\r\n]+} {
        set lastline $expect_out(0,string)
        exp_continue
    }
    close
    return $lastline
}
puts >>[bc 9487294387234/sqrt(394872394879847293847)]<<

I get this output...

spawn bc
9487294387234/sqrt(394872394879847293847)
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
9487294387234/sqrt(394872394879847293847)
477
>>477<<
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文