0
我必须在文本中着色一些单词,这很好用,但是当它已经着色时我有问题。当它的颜色我不想用不同的颜色再次着色。我的问题是,即使它已经着色,我的代码也会再次着色。在Bash中着色没有彩色文本
这里是我的代码:
var=$(echo -e $line | grep ".*[^m]${word}[^\][^e].*" | sed -e "s/${word}/${color}${word}${endColor}/g")
if(var -n);then
line=$var
f
它是一个脚本,是每个奇数的颜色,甚至是你要的颜色词。问题是当有一个词已经着色,我不想重新记录它。 输入可以是任何东西
这里是完整的代码
function GetColor {
if [ $1 == 'r' ];then
color=$red;
fi
if [ $1 == 'b' ];then
color=$blue;
fi
if [ $1 == 'g' ];then
color=$green;
fi
}
red=$'\e[31m'
green=$'\e[32m'
blue=$'\e[34m'
endColor=$'\e[0m'
a=0
color=""
word=""
while read input
do
radek=$input
for i in $*; do
if ((a% 2)); then
word=$i
var=$(echo -e $line | grep ".*[^m]${word}[^\][^e].*" | sed -e "s/${word}/${color}${word}${endColor}/g")
if(var -n);then
line=$var
fi
else
color=""
GetColor "$i"
fi
let "a += 1"
done
echo -e $line
exit
done
感谢您的帮助
请给我们一些样本数据的工作。 $ line的典型值是多少?鉴于样本输入,最好还包括必需的输出。祝你好运。 – shellter