在终端上打印并同时写入文件?

问题描述 投票:0回答:4

我有一个 shell 脚本,可以 grep 一些数据。我想将结果打印到文件中,但这样做会阻止结果显示在终端上。有没有一种方法既可以在屏幕上打印结果,又可以写入文件。 预先感谢。

linux shell terminal
4个回答
19
投票

将输出通过管道传输到

tee
命令。

示例:

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

请注意,

echo
的标准输出会被打印出来并写入由thr
tee
命令指定的文件中。


6
投票

请注意,您可以将

-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

1
投票

做你想做的事

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


0
投票

这是另一种不带 Tee 的方法。

echo "some content" > /dev/tty > log_file.txt
© www.soinside.com 2019 - 2024. All rights reserved.