9

最近、stderr によるテキスト出力を Linux (bash) 用に別の色にする解決策を提供する投稿を見つけました。

彼らは次のbashスクリプトスクリプトを作成しました

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

これにより、stderr からの出力の場合、黄色のテキストが出力されます。stdout は引き続き同じ色を出力します。

スクリプトは $PATH の color というディレクトリに保存されました。これにより、make または scons を使用してスクリプトを実行できるようになり、stderr からのすべてのテキストが黄色で強調表示されます。(33m を 31m に変更すると、テキストを赤くすることができます)

color make CPU=x64 

これは、コンパイル時にエラーを見つけるのに非常に役立ちます。

Windows コマンド シェルに使用できる同様のスクリプトはありますか?

注:役立つ場合は、Windowsコンピューターにsedをインストールしました。

4

3 に答える 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 の文字が 1 行に混在しています)。使用しているsed.exeのバージョンによって挙動が異なるのかもしれませんので、試してみてください。

これが機能しない場合は、最小限のcygwinインストールの使用を検討してください。スクリプトをテストcolor.shしたところ、.bat ファイルを起動でき、ストリームを混在させることなく正しく動作しました。私が使用した構文は次のとおりです。

./color.sh cmd /c test.bat
于 2012-04-12T06:46:15.837 に答える
4

純粋な Batch コマンドを使用して同等のソリューションを取得する方法を考案しました。つまり、Ansi、sed.exe などを使用せず、findstr のみを使用します。

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

この場合、別の色は stderr からの行全体に与えられるのではなく、findstr によって提供される行番号にのみ与えられますが、それは規定の要件には十分なはずです。stderr 行全体を別の色で表示する小さな補助 .exe プログラムをすぐに作成します。

于 2012-04-24T04:55:56.840 に答える
2

@MBu、@gnash117

は Windows コマンドであるため、名前を次のように変更colorco、次のように拡張しました。COLOR

:: color output - Displays stderr output in different color
::
:: Credits to MBu and gnash117 at http://stackoverflow.com/questions/10095886/change-text-output-color-on-windows-for-stderr#10118710
:: 
:: 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
于 2014-08-03T00:01:57.493 に答える