colcat

コンソール上に色のついた文字を表示させるシェルスクリプト colcat を作成してみました

 


 

#!/bin/bash
color=`getparstr $# "$*" "color"`
if [ -z $color ]
then
color=red
fi
#
# 30 => 黒 : Black
# 31 => 赤 : Red
# 32 => 緑 : Green
# 33 => 黄色 : Yellow
# 34 => 青 : Blue
# 35 => マゼンダ : Magenta
# 36 => シアン : Cyan
# 37 => 白 : White
#
case $color in
black) icol=30;;
red) icol=31;;
green) icol=32;;
yellow) icol=33;;
blue) icol=34;;
mazenta) icol=35;;
cyan) icol=36;;
white) icol=37;;
*) icol=39;;
esac

############ この下が肝 ############
printf "\033[0;"${icol}"m"
cat
printf "\033[0;39m"

 


 

colcat color=blue < hoge

 

のようにすると標準入力(この場合ファイルhoge)が青色文字で表示されるようになります。

例えば下のようなシェルスクリプトではエラーだけを赤色文字で表示させることができるようになります。

 

f:id:S_E_Hyphen:20171009110457p:plain

  


 

tmp=`mktemp ./XXX` # 一時ファイルの作成
for ( ( i=0; i<10; i++ ) )
do
exec 2> $tmp #標準出力は画面へ標準エラーはファイルへ

ls -la file${i} # コマンドの実行
# 正常終了すれば標準出力へ
# 異常が有れば一時ファイルに出力される

# 一時ファイルの内容を赤色文字で表示する
cat $tmp | colcat color=red
done
rm $tmp # 一時ファイルの消去