Print on terminal and into file simultaneously?

浪子不回头ぞ 提交于 2019-12-18 02:59:26

问题


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.


回答1:


Pipe your output to the tee command.

Example:

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

Note that the stdout of echo is printed out as well as written to the file specified by thr tee command.




回答2:


Note you can add the -a flag to tee to append to the output file

[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



回答3:


Does exactly your thing

http://linux.die.net/man/1/tee



来源:https://stackoverflow.com/questions/10782332/print-on-terminal-and-into-file-simultaneously

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!