尝试在TCL / Expect中使用puts时出错

时间:2016-07-09 09:45:29

标签: tcl expect

我有以下脚本:

#!/usr/bin/expect
set timeRegex "pengbumm"
expect {
   default { puts "nicht gefunden" }
   -re $timeRegex { puts "gefunden" }
}

如果我这样执行,我会收到错误:

martin:/tmp$ echo "pengibumm" |./bla.exp
error writing "stdout": bad file number while executing
"puts "nicht gefunden" "
invoked from within
"expect {
     default { puts "nicht gefunden" }
     -re $timeRegex { puts "gefunden" }
 }"
 (file "./bla.exp" line 4)
 martin:/tmp$ echo "pengbumm" |./bla.exp
 gefunden
 martin:/tmp$

为什么一个人投入工作而另一个人没有工作?我该如何解决这个问题?

我猜它与所有正在消耗的输入有关,但肯定不应该停止工作。

2 个答案:

答案 0 :(得分:2)

expect在这种情况下从stdout获得EOF后,似乎stdin关闭{/ p>}

$ echo | expect -c 'expect eof { puts eof }'
error writing "stdout": bad file number
    while executing
"puts eof "
    invoked from within
"expect eof { puts eof }"
$

不确定这是否是一个错误,但如果没有expect进程,通常不会使用spawn

更新

刚刚找到了解决方法。您可以putsstderr代替stdout

$ echo | expect -c 'expect eof { puts stderr eof }'
eof
$

答案 1 :(得分:0)

似乎expect假定通道输入/输出对不能是单向的,因此expect_user在收到stdin的EOF后关闭stdin和stdout。尽管这对于大多数spawn ed命令来说是有意义的,但对于当前脚本的stdin / stdout而言却没有意义。

除了pynexj的变通方法(使用puts stderr进行输出,因为stderr在stdin上收到EOF时expect_user并没有关闭它),看起来您可以使用Tcl gets或使用read命令而不是expect_user解决此问题:

set input [read stdin]
if [regexp {pengbumm} $input] then {
  puts "nicht gefunden"
} else {
  puts "gefunden"
}
相关问题