简体   繁体   English

TCL - 如何在屏幕上打印执行exec命令期间打印的消息?

[英]TCL - How to print on the screen th messages that where printed during execution of exec command?

Say I have want to execute a script or and executable file by printing runtime the output of execution. 假设我想通过打印运行时执行输出来执行脚本或可执行文件。

When I do: 当我做:

set log [exec ./executable_file]
puts $log

Then it waits a long time and then prints everything at once. 然后等待很长时间然后立即打印所有内容。 But I want runtime printing. 但我想要运行时打印。 How can I do this? 我怎样才能做到这一点?

Not perfect (as it require writing to external file): 不完美(因为它需要写入外部文件):

set log [exec executable_file | tee log.txt >@stdout]

The output will be displayed immediately, at the same time, saved to 'log.txt'. 输出将立即显示,同时保存为“log.txt”。 If you don't care about saving the output: 如果您不关心保存输出:

set log [exec executable_file >@stdout]

Use open "| ..." and asyncronous linewise reading from the returned descriptor, like this: 从返回的描述符中使用open "| ..."和异步行读取,如下所示:

proc ReadLine fd {
  if {[gets $fd line] < 0} {
    if {[chan eof $fd]} {
      chan close $fd
      set ::forever now
      return
    }
  }
  puts $line
}

set fd [open "| ./executable_file"]
chan configure $fd -blocking no
chan event $fd readable [list ReadLine $fd]

vwait forever

See this wiki page for more involved examples. 有关更多参与示例,请参阅此Wiki页面

In a real program you will probably already have an event loop running so there would be no need for a vwait specific to reading the output of one command. 在实际程序中,您可能已经运行了一个事件循环,因此不需要特定于读取一个命令输出的vwait

Also if you need to collect the output, not just [puts] each line after it has been read, you will pobably need to create a global (usually namespaced) variable, initialize it to "", pass its name as another argument to the callback procedure ( ReadLine here) and append the line to that variable's value. 此外,如果您需要收集输出,而不仅仅是[puts]读取后的每一行,您可能需要创建一个全局(通常是命名空间的)变量,将其初始化为“”,将其名称作为另一个参数传递给回调过程(此处为ReadLine )并将该行追加到该变量的值。

也许你可以在可执行文件之前在后台启动另一个进程,比如tail -f logfile.txt,并在这个logfile.txt中输出你的可执行文件的结果?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM