Tcl 如何从expect获得输出

Tcl 如何从expect获得输出,tcl,Tcl,我编写了一个脚本,用于发送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
命令

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)

如何解决这个问题。

问题是它与您期望的不匹配。(引用杰米·扎温斯基的话。)

试试这个:

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)]<<
包需要Expect
过程bc均衡{
繁殖bc;#就在我的路上。。。
发送$eq\r\n
设置超时1;#1秒
预期超时{}-re{[^\r\n]+}{
设置最后一行$expect\u out(0,字符串)
exp\u继续
}
关闭
返回$lastline
}

PUT> > [BC 9487294387234/SqRT(3307724987987249847)]部分问题是<代码> BC/<代码>没有任何提示。我得到这样的输出:d:tCLABL> TCLSH8BC.TCL BC 1.06版权1991年至1994年,1997, 1998, 2000免费软件基金会,这是免费软件,绝对没有保证。有关详细信息,请键入“保修”。9487294387234/平方米(394872394879847293847)4777@Mallikar字体我不知道怎么了。我尝试完全从上面的代码(即,切割-N粘贴到文件),它为我工作,如上所述。@ DONAI我没有得到总输出的结果BC 1.06版权1991年至1994年,1997, 1998, 2000免费软件基金会,这是免费软件,绝对没有担保。有关详细信息,请键入“保修”。9487294387234/sqrt(394872394879847293847)477>>7我想这可能是Windows版本expect的一个问题。愚蠢的是,你可以驾驶
bc
而根本不使用expect:
set f[open | bc r+];fconfigure$f-缓冲无;卖出9487294387234澳元/平方米(394872394879847293847);获取$f结果;接近$f;放入$result
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<<