set pipeline [open "|Certify.exe args" "r"]
fconfigure $pipeline -blocking false
fconfigure $pipeline -buffering none
fileevent $pipeline readable [list handlePipeReadable $pipeline]
proc handlePipeReadable {pipe} {
if {[gets $pipe line] >= 0} {
# Managed to actually read a line; stored in $line now
} elseif {[eof $pipe]} {
# Pipeline was closed; get exit code, etc.
if {[catch {close $pipe} msg opt]} {
set exitinfo [dict get $opt -errorcode]
} else {
# Successful termination
set exitinfo ""
}
# Stop the waiting in [vwait], below
set ::donepipe $pipe
} else {
puts ""
# Partial read; things will be properly buffered up for now...
}
}
vwait ::donepipe我尝试过在TCL代码中使用管道。但出于某些原因,我想将其转换为Spawn Expect机制。但我正在努力解决这个问题,并在这样做的时候遇到一些问题。有谁能帮帮我吗?
发布于 2019-06-28 20:20:09
Expect使得使用模式非常不同,它使用一种不同的方式与包装的程序交互,这更像是交互式使用的工作方式(它阻止了一整类与缓冲相关的bug,我怀疑这可能就是您要遇到的问题)。正因为如此,将东西转换过来并不是一个即兴的改变。下面是在一个简单案例中使用的基本模式:
package require Expect
# Note: different words become different arguments here
spawn Certify.exe args
expect "some sort of prompt string"
send "your input\r"; # \r is *CARRIAGE RETURN*
expect "something else"
send "something else\r"
expect eof
close当你可以设置超时,一次等待多个东西,等待模式和文字字符串等时,真正的复杂性出现了。但是从普通的Tcl做同样的事情(甚至忽略缓冲问题)是更多的工作。在几乎所有其他语言中,它也几乎总是更多的工作。
请注意,Expect不执行GUI自动化。只有命令行程序。GUI自动化是一个复杂得多的主题。
不可能对可能要做的事情给出一般性的描述,因为这在很大程度上取决于Certify.exe程序实际做了什么,以及如何交互地使用它。
https://stackoverflow.com/questions/56801395
复制相似问题