tee命令是否总是等待EOF?

时间:2020-03-06 14:28:22  来源:igfitidea点击:

我想将命令的输出记录到stdout以及日志文件中。我已经安装了Cygwin,并且正在尝试使用tee命令来完成此操作。

devenv mysolution.sln /build myproject "Release|Win32" | tee build.log

麻烦的是,tee似乎坚持等待文件结束,然后再向stdout或者日志文件输出任何内容。这消除了所有的要点,即拥有一个日志文件以供将来参考,还有一些" stdout"日志记录,因此我可以轻松地查看构建进度。

tee的选项似乎仅限于--append,-ignore-interrupts,-help和--version。那么,还有另一种方法可以实现我的目标吗?

解决方案

我们可以输出到文件,并在文件的末尾添加-f。

devenv mysolution.sln / build myproject" Release | Win32"> build.log&

tail -f build.log

自己写! (这里的要点是自动刷新($ |)设置是打开的,因此看到的每一行都立即被刷新。这也许是真正的tee命令所缺少的。)

#!/usr/bin/perl -w
use strict;
use IO::File;
$| = 1;
my @fhs = map IO::File->new(">$_"), @ARGV;
while (my $line = <STDIN>) {
    print $line;
    $_->print($line) for @fhs;
}
$_->close for @fhs;

我们可以根据需要调用脚本。我称之为" perlmilktee"! :-P

tee seems to insist on waiting for the
  end of file before outputting anything
  to either stdout or the log file.

这绝对不会发生,这会使Tee几乎毫无用处。这是我写的一个简单测试,已经通过测试,它绝对不是在等待eof。

$ cat test
#!/bin/sh
echo "hello"
sleep 5
echo "goodbye"

$ ./test | tee test.log
hello
<pause>
goodbye