STDOUT和STDERR在XTerm / Konsole下使用不同的颜色吗? [英] Can STDOUT and STDERR use different colors under XTerm / Konsole?
问题描述
是否可以实现?
我们需要 STDERR
流然后 STDOUT
)有不同的colo(u)r。例如红色。
我们使用 bash
,终端应为 Konsole $ c感谢您知道: - )
在bash脚本中创建一个函数:
color()(set -o pipefail;$ @2>& 1&&; 3 | sed $' ,\e [31m& \e [m,'& 2)3>& 1
$ b b
使用它:
$ color command -program -args
它会以红色显示命令的 stderr
继续阅读以了解其工作原理的解释。这个命令展示了一些有趣的功能。
-
color()...
—创建一个称为颜色的bash函数。 -
set -o pipefail
—这是一个shell选项,保留命令的错误返回码,该命令的输出通过管道传递到另一个命令。这在一个子shell中完成,它由括号创建,以便不更改外壳中的pipefail选项。 -
$ @
—将函数的参数作为新命令执行。$ @
等效于$ 1$ 2...
-
2>& 1
—将命令的stderr
重定向到stdout
,使其成为sed $ c $
1>< 3
的缩写,将stdout
重定向到新的临时文件描述符3
。3
稍后路由回stdout
。 -
sed ...
—由于上面的重定向,sed
的stdin
是stderr
。它的功能是用颜色代码包围每一行。 -
$'...'
一个bash结构, -
。
—匹配整行。 -
\e [31m
—导致以下字符为红色的ANSI转义序列 -
&
—sed
替换扩展到整个匹配字符串(本例中为整行)的字符。 -
\e [m
—用于重置颜色的ANSI转义序列。 -
>& 2
—1>< / c $ c>的缩写,重定向
sed
的stdout
到stderr
。 -
3& 1
—将临时文件描述符3重定向回
stdout
。
这是一个同样概念的扩展,也使STDOUT绿色:
function stdred()(
set -o pipefail;
($ @2>& 1&&; 3 | sed $' ,\e [31m& \e [m,'& 2)3& 1 \
| sed $'s,。*,\e [32m& m,'
)
Is it even achievable?
We need STDERR
(ie. other streams then STDOUT
) to have different colo(u)r. For example red.
We use bash
, terminal should be Konsole
(XTerm, gnome terminal or any usable).
Thanks if you know :-)
Here's a solution that combines some of the good ideas already presented.
Create a function in a bash script:
color()(set -o pipefail;"$@" 2>&1>&3|sed $'s,.*,\e[31m&\e[m,'>&2)3>&1
Use it like this:
$ color command -program -args
It will show the command's stderr
in red.
Keep reading for an explanation of how it works. There are some interesting features demonstrated by this command.
color()...
— Creates a bash function called color.set -o pipefail
— This is a shell option that preserves the error return code of a command whose output is piped into another command. This is done in a subshell, which is created by the parentheses, so as not to change the pipefail option in the outer shell."$@"
— Executes the arguments to the function as a new command."$@"
is equivalent to"$1" "$2" ...
2>&1
— Redirects thestderr
of the command tostdout
so that it becomessed
'sstdin
.>&3
— Shorthand for1>&3
, this redirectsstdout
to a new temporary file descriptor3
.3
gets routed back intostdout
later.sed ...
— Because of the redirects above,sed
'sstdin
is thestderr
of the executed command. Its function is to surround each line with color codes.$'...'
A bash construct that causes it to understand backslash-escaped characters.*
— Matches the entire line.\e[31m
— The ANSI escape sequence that causes the following characters to be red&
— Thesed
replace character that expands to the entire matched string (the entire line in this case).\e[m
— The ANSI escape sequence that resets the color.>&2
— Shorthand for1>&2
, this redirectssed
'sstdout
tostderr
.3>&1
— Redirects the temporary file descriptor3
back intostdout
.
Here's an extension of the same concept that also makes STDOUT green:
function stdred() (
set -o pipefail;
("$@" 2>&1>&3 | sed $'s,.*,\e[31m&\e[m,' >&2) 3>&1 \
| sed $'s,.*,\e[32m&\e[m,'
)
这篇关于STDOUT和STDERR在XTerm / Konsole下使用不同的颜色吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!