windows 如何将控制台输出重定向到文件并仍然在控制台中获取它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7612608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:47:27  来源:igfitidea点击:

How to redirect console output to file and STILL get it in the console?

windowsshellbatch-fileunix

提问by erezul

I want to run an ANT script which prompts the user for input, so it needs to be interactive through the console. at the same time I want to log the console content to a log file. I know I can use ant >build.log 2<&1which will redirect to file, but leave the console empty.

我想运行一个 ANT 脚本来提示用户输入,所以它需要通过控制台进行交互。同时我想将控制台内容记录到日志文件中。我知道我可以使用ant >build.log 2<&1which 将重定向到文件,但将控制台留空。

So, how can that be done? needed on windows and unix.

那么,如何做到这一点呢?在 windows 和 unix 上需要。

回答by lavinio

Use tee.

使用tee.

ant 2>&1|tee build.log

tee.exe is also available for Windows from http://unxutils.sourceforge.net/

tee.exe 也可从http://unxutils.sourceforge.net/获得 Windows

回答by derobert

You can use tee.

您可以使用tee.

Example:

例子:

$ echo "Hello, world" | tee /tmp/outfile
Hello, world
$ cat /tmp/outfile
Hello, world

teewrites its stdin to both stdout as well as one or more files given on the command line.

tee将其标准输入写入标准输出以及命令行上给出的一个或多个文件。