在Bash中不着色的彩色文本 [英] Coloring not colored text in Bash

查看:106
本文介绍了在Bash中不着色的彩色文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要上色的文本,该作品好一些的话,但我有一个问题,当它已经着色。当它的彩色我不想用不同的颜色再次颜色故。我的问题是,我的code再次颜色,即使它已经是彩色的。

I have to colorize some words in the text, that works good, but I have a problem when its already colored. When its colored I dont want to colour it again with different color. My problem is that my code color it again even if it is already colored.

下面是我的code:

var=$(echo -e $line | grep ".*[^m]${word}[^\][^e].*" | sed -e "s/${word}/${color}${word}${endColor}/g")
if(var -n);then
line=$var
f


它是一个脚本,是每一个奇怪的颜色,甚至是你要的颜色词。问题是,当有一个已着色的话,我不希望它recolore。
输入可以是任何东西。


Its a script where is every odd the color and even is word you want to color. The problem is when there is a word that is already colored and I dont want to recolore it. Input could be anything

这里到处是code

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

感谢您的帮助。

推荐答案

这是我的版本,但我刚才提出的脚本运行没有错误。我不知道是什么问题,但已经着色字的颜色也不会改变。

This is my version, but I've just made the script run without errors. I'm not sure what the problem is, but the colour of already coloured words are not changed.

我怀疑

if(var -n);then

我把它纠正,以

  if [ -n "$var" ]; then

下面的脚本

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
  line=$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 [ -n "$var" ]; then
        line=$var
      fi
    else
      color=""
      GetColor "$i"
    fi
    let "a += 1"
  done

  echo -e $line
  exit
done

这篇关于在Bash中不着色的彩色文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆