我想将一个程序的输出(stdout)保存到一个文件中,但是仍然能够实时地在屏幕上看到输出。
我需要Linux和Windows的解决scheme。
在Linux中,您可以使用tee实用程序。
在Windows中,PowerShell包含一个tee cmdlet 。 对于cmd ,您将需要下载并安装单独的 实用程序 。
在Linux和类似系统上:
program | tee filename
tee程序将任何进入其标准input的东西(如cat )发送到标准输出,并将其写入指定的文件。
另一种获得相同效果的方法是
program >filename 2>/dev/null & tail -f filename
这将在后台运行程序,将其标准输出redirect到文件,然后使用tail -f实时跟踪正在写入文件的数据(或者几乎是这样,也许是一秒的延迟)。 2>/dev/null使得标准错误stream消失,所以它不会干扰tail -f的输出。
Linux: command | tee logfile command | tee logfile
Windows:安装cygwin,然后运行: command | tee logfile command | tee logfile
对于Windows,你可以这样做:
dir> directory.txt&type directory.txt
你应该在哪里replacedir 。
如果输出滚动得太快,使用:
dir> c:\ directory.txt&type c:\ directory.txt | 更多
但是,我不认为这会实时显示文件的内容。 你必须等到这个过程结束。
在Windows中,如果您下载它的GnuWin32版本,则可以使用tee 。 它和linux应用程序一样,来自coreutils软件包 。
有一点要注意, tee有它自己的返回代码,所以你将不能注意到从pipe理进程中的错误( 除非你在linux中,然后有办法解决 )。