Linux 如何将 stderr 和 stdout 重定向到脚本同一行中的不同文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7901517/
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 stderr and stdout to different files in the same line in script?
提问by user784637
I know this much:
我知道这么多:
$ command 2>> error
$ command 1>> output
Is there any way I can output the stderr to the error file and output stdout to the output file in the same line of bash?
有什么办法可以将stderr输出到错误文件并将stdout输出到bash的同一行中的输出文件吗?
采纳答案by Sujoy
Just add them in one line command 2>> error 1>> output
只需将它们添加在一行中 command 2>> error 1>> output
However, note that >>
is for appending if the file already has data. Whereas, >
will overwrite any existing data in the file.
但是,请注意,>>
如果文件已经有数据,则用于追加。而,>
将覆盖文件中的任何现有数据。
So, command 2> error 1> output
if you do not want to append.
所以,command 2> error 1> output
如果你不想追加。
Just for completion's sake, you can write 1>
as just >
since the default file descriptor is the output. so 1>
and >
is the same thing.
只是为了完成起见,您可以1>
像这样编写,>
因为默认文件描述符是输出。所以1>
和>
是一回事。
So, command 2> error 1> output
becomes, command 2> error > output
于是,command 2> error 1> output
变成,command 2> error > output
回答by Didier Trosset
Like that:
像那样:
$ command >>output 2>>error
回答by ztank1013
Or if you like to mix outputs (stdout & stderr) in one single file you may want to use:
或者,如果您想在一个文件中混合输出(stdout 和 stderr),您可能需要使用:
command > merged-output.txt 2>&1
回答by Quintus.Zhou
Try this:
尝试这个:
your_command 2>stderr.log 1>stdout.log
More information
更多信息
The numerals 0
through 9
are file descriptorsin bash.
0
stands for standard input, 1
stands for standard output, 2
stands for standard error. 3
through 9
are spare for any other temporary usage.
该数字0
经过9
是文件描述符在bash。
0
代表标准输入,1
代表标准输出,2
代表标准错误。3
通过9
是备用的任何其他临时用途。
Any file descriptor can be redirected to a file or to another file descriptor using the operator >
. You can instead use the operator >>
to appends to a file instead of creating an empty one.
任何文件描述符都可以使用运算符重定向到一个文件或另一个文件描述符>
。您可以改为使用运算符>>
附加到文件而不是创建一个空文件。
Usage:
用法:
file_descriptor > filename
file_descriptor > &file_descriptor
Please refer to Advanced Bash-Scripting Guide: Chapter 20. I/O Redirection.
回答by rahogaboom
Multiple commands' output can be redirected. This works for either the command line or most usefully in a bash script. The -s
directs the password prompt to the screen.
可以重定向多个命令的输出。这适用于命令行或在 bash 脚本中最有用。该-s
指示密码提示屏幕。
Hereblock cmds stdout/stderr are sent to seperate files and nothing to display.
Hereblock cmds stdout/stderr 被发送到单独的文件,没有任何显示。
sudo -s -u username <<'EOF' 2>err 1>out
ls; pwd;
EOF
Hereblock cmds stdout/stderr are sent to a single file and display.
Hereblock cmds stdout/stderr 被发送到单个文件并显示。
sudo -s -u username <<'EOF' 2>&1 | tee out
ls; pwd;
EOF
Hereblock cmds stdout/stderr are sent to separate files and stdout to display.
Hereblock cmds stdout/stderr 被发送到单独的文件和 stdout 以显示。
sudo -s -u username <<'EOF' 2>err | tee out
ls; pwd;
EOF
Depending on who you are(whoami) and username a password may or may not be required.
根据您是谁(whoami)和用户名,可能需要也可能不需要密码。