管道输出和错误在shell脚本两个不同的进程? [英] pipe stdout and stderr to two different processes in shell script?
问题描述
我有一个pipline正是这样做
命令1 |命令2
那么,是command1的标准输出去命令2,而标准错误命令1到终端(或其它地方的外壳stdout是)。
我怎么能是command1管标准错误到第三个过程(指令代码
),而标准输出仍然会命令2?
使用另一个文件描述符
{命令1 2 - ;及3 |命令2; } 3 GT;&放大器; 1 1 GT;和2 |命令3
您可以使用最多7个其他文件描述符:3至9结果
如果您想了解更多的解释,请你,我可以解释; - )
测试
{{回声;回声>&安培; 2 B; } 2 - ;和3 | SED>和2的/ $ / 1 /'; } 3 GT;&放大器; 1 1 GT;和2 | SED的/ $ / 2 /'
输出:
B2
A1
示例
产生两个日志文件:结果
1. 标准错误
仅结果
2. 标准错误
和标准输出
{{{命令2 - ;&放大器; 1 1 GT;及3; } |三通ERR-only.log; } 3>&放大器; 1; }> ERR-和stdout.log
如果命令
是回声标准输出;回声标准错误>和2
那么我们可以测试它这样的:
$ {{{回声出>及3;回声ERR>&放大器; 1;} |三通ERR-only.log;} 3 GT;&放大器; 1;}> ERR-和stdout.log
$头ERR-only.log ERR-和stdout.log
==> ERR-only.log< ==
呃==> ERR-和stdout.log&下; ==
出
呃
I've a pipline doing just
command1 | command2
So, stdout of command1 goes to command2 , while stderr of command1 go to the terminal (or wherever stdout of the shell is).
How can I pipe stderr of command1 to a third process (command3
) while stdout is still going to command2 ?
Use another file descriptor
{ command1 2>&3 | command2; } 3>&1 1>&2 | command3
You can use up to 7 other file descriptors: from 3 to 9.
If you want more explanation, please ask, I can explain ;-)
Test
{ { echo a; echo >&2 b; } 2>&3 | sed >&2 's/$/1/'; } 3>&1 1>&2 | sed 's/$/2/'
output:
b2
a1
Example
Produce two log files:
1. stderr
only
2. stderr
and stdout
{ { { command 2>&1 1>&3; } | tee err-only.log; } 3>&1; } > err-and-stdout.log
If command
is echo "stdout"; echo "stderr" >&2
then we can test it like that:
$ { { { echo out>&3;echo err>&1;}| tee err-only.log;} 3>&1;} > err-and-stdout.log
$ head err-only.log err-and-stdout.log
==> err-only.log <==
err
==> err-and-stdout.log <==
out
err
这篇关于管道输出和错误在shell脚本两个不同的进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!