在Windows上更改stderr的文本输出颜色

10

我最近发现了一篇文章,提供了一种解决方案,可以让Linux (bash)中由stderr输出的文本显示为不同的颜色。

他们创建了以下Bash脚本:

#!/bin/bash
{ $* 2>&1>&3|sed 's,.*,\x1B[33m&\x1B[0m,'>&2;} 3>&1

这会导致stderr输出的文本呈黄色。stdout仍然以相同的颜色打印。
该脚本保存在名为color的$PATH目录中。这使我能够使用make或scons运行脚本,并将所有来自stderr的文本都以黄色突出显示(通过将33m更改为31m,可以使文本变为红色)。
color make CPU=x64 

这对于编译时查找错误非常有用。

是否有类似的脚本可以用于Windows cmd shell?

注意:如果有需要,我已经在我的Windows电脑上安装了sed。

3个回答

6
关于在Windows的cmd.exe下支持ANSI转义代码,请参阅ansicon。将您的重定向逻辑转换为cmd.exe语法后,我准备了以下color.bat文件:
@Echo Off
(((%* 1>&3) 2>&1) | "c:\Program Files (x86)\GnuWin32\bin\sed.exe" "s,.*,\x1B[33m&\x1B[0m," 1>&2) 3>&1

很遗憾,有些行的stdout和stderr字符混合在一起(可能取决于使用的sed.exe版本),因此请尝试一下。
如果这样不行,请考虑使用最小 cygwin 安装包。我测试了您的 color.sh 脚本,并且能够启动.bat文件并且没有混合流的问题。我使用的语法是:
./color.sh cmd /c test.bat

谢谢,我现在不在电脑旁边,但我会尝试一下并让你知道结果。 - gnash117
非常感谢,由于我已经将sed添加到了PATH中,所以我不必指定完整路径,留下@Echo Off。 (((%* 1>&3) 2>&1) | "sed.exe" "s,.*,\x1B[31m&\x1B[0m," 1>&2) 3>&1 - gnash117

4

我想出了一种使用纯批处理命令获取等效解决方案的方法,即不使用Ansi、sed.exe等,只使用findstr:

any_command 2>&1 1>&3 | findstr /N /A:4E "^"

在这种情况下,不是将stderr的整个行赋予不同颜色,而是仅将由findstr提供的行号赋予不同颜色,然而,这应该足以满足所述要求。我很快会编写一个小型辅助.exe程序,它将以另一种颜色显示整个stderr行。

在我的Win7上,这个命令确实可以给行着色,但是只有输出stderr,stdout没有显示。 - Martin Ba

2

@MBu,@gnash117

我将color重命名为co,因为COLOR是Windows命令,并且我进行了扩展:

:: color output - Displays stderr output in different color
::
:: Credits to MBu and gnash117 at https://dev59.com/oWLVa4cB1Zd3GeqPxIru
:: 
:: Requires:
::   - http://sourceforge.net/projects/mingw/files/MSYS/
::   - http://adoxa.altervista.org/ansicon/
::   - http://www.autohotkey.com/
::
@echo off

if "%1"=="" goto :Help

:: 1;31 ... intense red foreground (see https://github.com/adoxa/ansicon/blob/master/sequences.txt for more colors)
:: \x07 ... BEL, but doesn't work :-(
(((%* 1>&3) 2>&1) | sed "s/.*/\x07\x1B[1;31m&\x1B[0m/" 1>&2) 3>&1
goto :EOF

:Help
setlocal
::------------------------------------------------
:: Adapt these in pairs according to your likings
set invokeKeys=[Shift]+[Enter]
set invokeHotkeys=+Enter
set invokeAfterRemoveKeys=[Alt]+[Enter]
set invokeAfterRemoveHotkeys=!Enter
set removeKeys=[Alt]+[c]
set removeHotkeys=!c
::-----------------------------------------------
set invokeText=invokes the entered command after preceding it with '%0 '
set invokeAfterRemoveText=invokes the entered command after removing the first three characters from the beginning
set removeText=removes the first three characters from the command line
echo Colors a command's stderr output as defined in %~f0
echo Usage:
echo   - Preceed a command with '%0 ' (e.g.: 'co dir not.existing.file' will show the resulting error message in a different color)
echo   - If the AutoHotkey script below is active:
echo     - %invokeKeys% ... %invokeText%
echo     - %invokeAfterRemoveKeys% ... %invokeAfterRemoveText%
echo     - %removeKeys% ... %removeText%
echo(
echo       The latter two are useful when using the command line history and having used %invokeKeys% before.
echo(
echo     Enabled by AutoHotkey script:
echo(
echo       #IfWinActive ahk_class ConsoleWindowClass
echo       ; %invokeText%
echo       %invokeHotkeys%::Send {Home}%0 {Enter}
echo       ; %invokeAfterRemoveText%
echo       %invokeAfterRemoveHotkeys%::SendInput {Home}{Del 3}{Enter}
echo       ; %removeText%
echo       %removeHotkeys%::SendInput {Home}{Del 3}{End}
echo       #IfWinActive
endlocal
:EOF

我最终将脚本命名为hilite(highlight的拼写错误),因为我也遇到了颜色问题。不过我真的很喜欢这个脚本。 :) - gnash117

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接