我怎样才能“冲洗”stdout
of the exec
命令到我的脚本stdout
没有“等待”结果执行返回?
例如在下面的脚本中我想要git clone
输出立即出现在我的脚本上下文中:
#!/usr/bin/tclsh
# git outputs progress of the clone download but it isn't visible in this script's context. How can I flush it?
exec git clone /path/to/some/repo.git
我想我需要某种组合pipe "|"
and tee
和文件重定向。
似乎无法正确处理。
要立即获取输出,您需要将子命令作为管道打开。正确的(而不是quite显而易见,对此我们深表歉意)这样做的方法是使用以下结构open |[list …]
:
set mypipeline [open |[list git clone /path/to/some/repo.git]]
# Now to read it by line; we'll just print it out with a line number to show off
set linenum 0
while {[gets $mypipeline line] >= 0} {
puts "Line [incr linenum]: $line"
}
# Close the pipe now we're at EOF
close $mypipeline
但请注意,有些程序(我不知道是否git
是之一)在管道中运行时改变它们的行为,缓冲它们的输出,直到它们具有完整的缓冲区值。 (当输出不发送到终端时,这是 C 运行时默认工作方式的一部分。)如果这是一个问题,您将必须使用 Expect 来运行。这是一个足够大的主题,您应该寻找(并在必要时提出)一个单独的问题;唉,这在复杂性上是一个很大的变化。
另请注意,git 很可能会写入其标准错误(如本问题所述,从 PHP exec() 函数读取 git Push 的输出 https://stackoverflow.com/questions/13389494/reading-output-of-git-push-from-php-exec-function)因此您可能需要将标准错误合并到捕获的标准输出中(如exec https://www.tcl.tk/man/tcl8.6/TclCmd/exec.htm手册页)。
set output [exec git clone /path/to/some/repo.git 2>@1]
set mypipeline [open |[list git clone /path/to/some/repo.git 2>@1]]
# ...
也可以执行读/写管道,但更复杂。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)