使用 bash,如何将标准错误传送到另一个进程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1507816/
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
With bash, how can I pipe standard error into another process?
提问by paxdiablo
It's well known how to pipe the standard ouput of a process into another processes standard input:
众所周知,如何将一个进程的标准输出通过管道传输到另一个进程的标准输入中:
proc1 | proc2
But what if I want to send the standard error of proc1 to proc2 and leave the standard output going to its current location? You would think bash
would have a command along the lines of:
但是,如果我想将 proc1 的标准错误发送到 proc2 并将标准输出留在其当前位置怎么办?你会认为bash
会有一个类似的命令:
proc1 2| proc2
But, alas, no. Is there any way to do this?
但是,唉,没有。有没有办法做到这一点?
回答by Scot
There is also process substitution. Which makes a process substitute for a file.
You can send stderr
to a file as follows:
还有过程替换。这使得一个进程替代了一个文件。
您可以stderr
按如下方式发送到文件:
process1 2> file
But you can substitute a process for the file as follows:
但是您可以按如下方式替换文件的进程:
process1 2> >(process2)
Here is a concrete example that sends stderr
to both the screen and appends to a logfile
这是一个发送stderr
到屏幕并附加到日志文件的具体示例
sh myscript 2> >(tee -a errlog)
回答by paxdiablo
You can use the following trick to swapstdout
and stderr
. Then you just use the regular pipe functionality.
您可以使用以下技巧来交换stdout
和stderr
。然后您只需使用常规管道功能。
( proc1 3>&1 1>&2- 2>&3- ) | proc2
Provided stdout
and stderr
both pointed to the same place at the start, this will give you what you need.
提供stdout
并且stderr
两者在开始时都指向同一个地方,这将为您提供所需的东西。
What the x>y
bit does is to change file handle x
so it now sends its information to where file handle y
currently points. For our specific case:
该x>y
位的作用是更改文件句柄,x
以便它现在将其信息发送到文件句柄y
当前指向的位置。对于我们的具体情况:
3>&1
creates a newhandle3
which will output to the currenthandle1
(original stdout), just to save it somewhere for the final bullet point below.1>&2
modifies handle1
(stdout) to output to the currenthandle2
(original stderr).2>&3-
modifies handle2
(stderr) to output to the currenthandle3
(original stdout) then closes handle3
(via the-
at the end).
3>&1
创建一个将输出到当前句柄(原始标准输出)的新句柄,只是为了将其保存在某处以用于下面的最后一个要点。3
1
1>&2
修改句柄1
(stdout) 以输出到当前句柄2
(原始 stderr)。2>&3-
修改句柄2
(stderr)以输出到当前句柄3
(原始标准输出)然后关闭句柄3
(通过-
最后)。
It's effectively the swap command you see in sorting algorithms:
它实际上是您在排序算法中看到的交换命令:
temp = value1;
value1 = value2;
value2 = temp;
回答by Paused until further notice.
Bash 4 has this feature:
Bash 4 有这个功能:
If `|&' is used, the standard error of command1 is connected to command2's standard input through the pipe; it is shorthand for 2>&1 |. This implicit redirection of the standard error is performed after any redirections specified by the command.
如果使用`|&',则command1的标准错误通过管道连接到command2的标准输入;它是 2>&1 | 的简写。标准错误的这种隐式重定向在命令指定的任何重定向之后执行。
zsh also has this feature.
zsh 也有这个功能。
--
——
With other/older shells, just enter this explicitly as
对于其他/较旧的外壳,只需明确输入
FirstCommand 2>&1 | OtherCommand
第一个命令 2>&1 | 其他命令
回答by kccqzy
Swapping is great as it solves the problem. Just in case you do not even need the original stdout, you can do it this way:
交换很棒,因为它解决了问题。以防万一您甚至不需要原始标准输出,您可以这样做:
proc1 2>&1 1>/dev/null | proc2
The order is vital; you would not want:
秩序至关重要;你不会想要:
proc1 >/dev/null 2>&1 | proc1
As this will redirect everything to /dev/null
!
因为这会将所有内容重定向到/dev/null
!
回答by sbingner
None of these really worked very well. The best way I found do do what you wanted is:
这些都没有真正奏效。我发现做你想做的最好的方法是:
(command < input > output) 2>&1 | less
This only works for cases where command
does not need keyboard input. eg:
这仅适用于command
不需要键盘输入的情况。例如:
(gzip -d < file.gz > file) 2>&1 | less
would put gzip errors into less
会将 gzip 错误放入 less