Linux 同时在终端和文件中打印?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10782332/
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-08-06 06:32:46  来源:igfitidea点击:

Print on terminal and into file simultaneously?

linuxshellterminal

提问by Xander

I have a shell script that greps some data.. I want to print the result into a file, but doing that prevents the result being displayed on the terminal. Is there a way that can both print the result on the screen and also write into a file. Thanks in advance.

我有一个用于 grep 一些数据的 shell 脚本。我想将结果打印到一个文件中,但这样做会阻止结果显示在终端上。有没有办法既可以在屏幕上打印结果,也可以写入文件。提前致谢。

采纳答案by Shawn Chin

Pipe your output to the teecommand.

将您的输出通过管道传输到tee命令。

Example:

例子:

[me@home]$ echo hello | tee out.txt
hello
[me@home]$ cat out.txt 
hello

Note that the stdout of echois printed out as well as written to the file specified by thr teecommand.

请注意,输出的标准输出echo以及写入 thrtee命令指定的文件。

回答by Quamis

回答by J00MZ

Note you can add the -aflag to teeto append to the output file

请注意,您可以将-a标志添加到tee以附加到输出文件

[me@home]$ echo hello | tee out.txt
hello
[me@home]$ echo hello again | tee -a out.txt
hello again
[me@home]$ cat out.txt
hello
hello again