shell脚本定义输出字符颜色

#-------------------定义输入颜色---------------------#
  RED='\033[1;31m'
  GREEN='\033[1;32m'
  YELLOW='\033[1;33m'
  BLUE='\033[1;34m'
  NORMAL='\033[0m'
  RedBG='\033[1;41;37m'
  GreenBG='\033[1;42;37m'
  YellowBG='\033[1;43;37m'

 

  Screen=$(stty -F /dev/console size|awk '{print $2}')
#如果算出来的值为空,就默认赋值为80.
  Columns=${Screen:-80}
#减12是指减掉"[ $RED File $NORMAL ]"所占的字符.
  SPACOL=$[$Columns-12]

GreenChar() {
  String=$1
#计算出真正的空白数,$Columns-12后的结果再减$1的字符数.
  RTSPA=$[${SPACOL}-${#String}]
  echo -en "$GREEN${String}$NORMAL"
  for I in `seq 1 $RTSPA`;do #如果I在1到68,则输出------
#出输入空白

#seq 1 $RTSPA的输出结果

#

    echo -n "-" #echo -n是不输出换行符
  done
  echo -e "[$GREEN OK $NORMAL]" # 输出结果[ OK ],-e允许反斜杠\的转义
}

RedChar() {
  String=$1
  RTSPA=$[${SPACOL}-${#String}]
  echo -en "$RED${String}$NORMAL"
  for I in `seq 1 $RTSPA`;do
    echo -n "-"
  done
  echo -e "[$RED Failed $NORMAL]" #同理,一行的最后字符是[ Failed ]
}

YellowChar() {
  String=$1
  RTSPA=$[${SPACOL}-${#String}]
  echo -en "$YELLOW${String}$NORMAL"
  for I in `seq 1 $RTSPA`;do
    echo -n "-"
  done
  echo -e "[$YELLOW Warning $NORMAL]" #同理,一行的最后字符是[ Warning ]
}

GreenBGChar() {

  echo ""
  echo -e "[[ $GreenBG $1 $NORMAL ]]"
  echo ""
}

RedBGChar() {

  echo ""
  echo -e "[[ $RedBG $1 $NORMAL ]]"
  echo ""
}

YellowBGChar() {

  echo ""
  echo -e "[[ $YellowBG $1 $NORMAL "
  echo ""
}

BG的输出结果

最终的字符效果

 

posted @ 2018-03-12 17:49  勤奋的雪雪大人  阅读(294)  评论(0编辑  收藏  举报