Linux 如何将输出重定向到文件和标准输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/418896/
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
How to redirect output to a file and stdout
提问by SCdF
In bash, calling foo
would display any output from that command on the stdout.
在 bash 中,调用foo
将在标准输出上显示该命令的任何输出。
Calling foo > output
would redirect any output from that command to the file specified (in this case 'output').
调用foo > output
会将来自该命令的任何输出重定向到指定的文件(在本例中为“输出”)。
Is there a way to redirect output to a file andhave it display on stdout?
有没有办法将输出重定向到文件并将其显示在标准输出上?
采纳答案by Zoredache
The command you want is named tee
:
您想要的命令名为tee
:
foo | tee output.file
For example, if you only care about stdout:
例如,如果您只关心标准输出:
ls -a | tee output.file
If you want to include stderr, do:
如果要包含 stderr,请执行以下操作:
program [arguments...] 2>&1 | tee outfile
2>&1
redirects channel 2 (stderr/standard error) into channel 1 (stdout/standard output), such that both is written as stdout. It is also directed to the given output file as of the tee
command.
2>&1
将通道 2(标准错误/标准错误)重定向到通道 1(标准输出/标准输出),这样两者都被写入为标准输出。它也被定向到tee
命令的给定输出文件。
Furthermore, if you want to appendto the log file, use tee -a
as:
此外,如果要附加到日志文件,请使用tee -a
:
program [arguments...] 2>&1 | tee -a outfile
回答by Chuck Phillips
Using tail -f output
should work.
使用tail -f output
应该有效。
回答by kal
tee is perfect for this, but this will also do the job
T恤非常适合这个,但这也可以完成这项工作
ls -lr / > output | cat output
回答by Matthew Alpert
$ program [arguments...] 2>&1 | tee outfile
2>&1
dumps the stderr and stdout streams.
tee outfile
takes the stream it gets and writes it to the screen and to the file "outfile".
2>&1
转储 stderr 和 stdout 流。
tee outfile
获取它获取的流并将其写入屏幕和文件“outfile”。
This is probably what most people are looking for. The likely situation is some program or script is working hard for a long time and producing a lot of output. The user wants to check it periodically for progress, but also wants the output written to a file.
这可能是大多数人正在寻找的。可能的情况是某个程序或脚本长时间工作并产生大量输出。用户希望定期检查进度,但也希望将输出写入文件。
The problem (especially when mixing stdout and stderr streams) is that there is reliance on the streams being flushed by the program. If, for example, all the writes to stdout are notflushed, but all the writes to stderr areflushed, then they'll end up out of chronological order in the output file and on the screen.
问题(尤其是在混合 stdout 和 stderr 流时)是依赖于程序刷新的流。例如,如果对 stdout 的所有写入都未刷新,但对 stderr 的所有写入都已刷新,则它们将在输出文件和屏幕上按时间顺序结束。
It's also bad if the program only outputs 1 or 2 lines every few minutes to report progress. In such a case, if the output was not flushed by the program, the user wouldn't even see any output on the screen for hours, because none of it would get pushed through the pipe for hours.
如果程序每几分钟只输出 1 或 2 行来报告进度,那也很糟糕。在这种情况下,如果程序没有刷新输出,用户甚至在几个小时内都不会在屏幕上看到任何输出,因为在几个小时内它都不会被推送到管道中。
Update: The program unbuffer
, part of the expect
package, will solve the buffering problem. This will cause stdout and stderr to write to the screen and file immediately and keep them in sync when being combined and redirected to tee
. E.g.:
更新:程序unbuffer
,expect
包的一部分,将解决缓冲问题。这将导致 stdout 和 stderr 立即写入屏幕和文件,并在组合和重定向到tee
. 例如:
$ unbuffer program [arguments...] 2>&1 | tee outfile
回答by O.Badr
回答by tsenapathy
Another way that works for me is,
另一种对我有用的方法是,
<command> |& tee <outputFile>
as shown in gnu bash manual
如gnu bash 手册中所示
Example:
例子:
ls |& tee files.txt
If ‘|&' is used, command1's standard error, in addition to its standard output, is connected to command2's standard input through the pipe; it is shorthand for 2>&1|. This implicit redirection of the standard error to the standard output is performed after any redirections specified by the command.
如果使用'|&' ,除了它的标准输出之外,command1 的标准错误通过管道连接到command2 的标准输入;它是2>&1| 的简写。标准错误到标准输出的这种隐式重定向是在命令指定的任何重定向之后执行的。
For more information, refer redirection
有关更多信息,请参阅重定向
回答by nitinr708
Something to add ...
有什么要补充的...
The package unbuffer has support issues with some packages under fedora and redhat unix releases.
在 fedora 和 redhat unix 版本下,包 unbuffer 对某些包有支持问题。
Setting aside the troubles
抛开烦恼
Following worked for me
以下为我工作
bash myscript.sh 2>&1 | tee output.log
回答by jorfus
Bonus answer since this use-case brought me here:
奖金答案,因为这个用例把我带到了这里:
In the case where you need to do this as some other user
如果您需要以其他用户身份执行此操作
echo "some output" | sudo -u some_user tee /some/path/some_file
Note that the echo will happen as you and the file write will happen as "some_user" what will NOTwork is if you were to run the echo as "some_user" and redirect the output with >> "some_file" because the file redirect will happen as you.
请注意,回声会发生在你和文件写入会发生的“some_user”会什么不工作,如果你要运行回声为“some_user”,并与输出重定向>>“SOME_FILE”,因为文件重定向会发生像你一样。
Hint: tee also supports append with the -a flag, if you need to replace a line in a file as another user you could execute sed as the desired user.
提示:tee 还支持附加 -a 标志,如果您需要以另一个用户身份替换文件中的一行,您可以以所需用户身份执行 sed。
回答by kiran sai
< command > |& tee filename
# this will create a file "filename" with command status as a content, If a file already exists it will remove existed content and writes the command status.
< command > |& tee filename
# 这将创建一个以命令状态为内容的文件“文件名”,如果文件已经存在,它将删除现有内容并写入命令状态。
< command > | tee >> filename
# this will append status to the file but it doesn't print the command status on standard_output (screen).
< command > | tee >> filename
# 这会将状态附加到文件,但不会在标准输出(屏幕)上打印命令状态。
I want to print something by using "echo" on screen and append that echoed data to a file
我想通过在屏幕上使用“回声”来打印一些东西并将回显的数据附加到文件中
echo "hi there, Have to print this on screen and append to a file"
回答by Bruno BEAUFILS
You can do that for your entire script by using something like that at the beginning of your script :
您可以通过在脚本开头使用类似的内容来为整个脚本执行此操作:
#!/usr/bin/env bash
test x = x$'\x00' && shift || { set -o pipefail ; ( exec 2>&1 ; ##代码## $'\x00' "$@" ) | tee mylogfile ; exit $? ; }
# do whaetever you want
This redirect both stderr and stdout outputs to the file called mylogfile
andlet everything goes to stdout at the same time.
这两种重定向标准错误和标准输出输出到文件称为mylogfile
并且让一切都到stdout在同一时间。
It is used some stupid tricks :
它使用了一些愚蠢的技巧:
- use
exec
without command to setup redirections, - use
tee
to duplicates outputs, - restart the script with the wanted redirections,
- use a special first parameter (a simple
NUL
character specified by the$'string'
special bash notation) to specify that the script is restarted (no equivalent parameter may be used by your original work), - try to preserve the original exit status when restarting the script using the
pipefail
option.
- 使用
exec
不带命令来设置重定向, - 用于
tee
复制输出, - 使用所需的重定向重新启动脚本,
- 使用特殊的第一个参数(
NUL
由$'string'
特殊 bash 表示法指定的简单字符)来指定重新启动脚本(您的原始作品可能不会使用等效参数), - 使用该
pipefail
选项重新启动脚本时,尝试保留原始退出状态。
Ugly but useful for me in certain situations.
丑陋但在某些情况下对我有用。