Shell output function with color text

Source: Internet
Author: User
Some monitoring scripts or restart services. The failure is successfully output in red or green. A function is used to encapsulate this function. Function: receives string parameters, changes the OK in the received parameter [] to red, and the FAILD to green. For example, "success": "failure": Put the Code directly #! /Bin/bashaction () {MSG = $1 COLOER = 'echo $1

Some monitoring scripts or restart services. The failure is successfully output in red or green.

A function is used to encapsulate this function.

Function: receives string parameters, changes the OK in the received parameter [] to red, and the FAILD to green. For example:

Successful:


#!/bin/bashaction(){MSG=$1COLOER=`echo $1|sed 's#^.*\[\(.*\)\].*#\1#g'`BASE=`echo $1|sed 's#\(^.*\)\[.*]#\1#g'`if[ "OK"!= "$COLOER"];thenecho -e "${BASE} [\e[0;31;1m  $COLOER  \e[0m]"elseecho -e "${BASE} [\e[1;32m $COLOER \e[0m]"fi}STAT=`curl -I -s 1www.baidu.com|grep HTTP|awk '{print $2}'`[ -z $STAT ]&&STAT=500if[ "200"-eq "$STAT"];thenaction "THE WEB STATUS IS  ............ [OK]"exit 0elseaction "THE WEB STATUS IS  ............ [FAILD]"exit 1fi

You can optimize the above action function and encode the color information as a parameter. More convenient call.

For example, action "blue" "the web status is [OK]". If you do not have this requirement, you will not write it.


Okay, it's idle, So I optimized the action function.

Only the green and red colors are encoded. Action "green" "the web status is [OK]", called in this way.

Code:

action(){REG_RED="\e[0;31;1m"REG_GREEN="\e[1;32m"RES="\e[0m"COLOER=""case"$1"inred|RED)COLOER=$REG_RED;;green|GREEN)COLOER=$REG_GREEN;;esacCOLOERTEXT=`echo $2|sed 's#^.*\[\(.*\)\].*#\1#g'`BASE=`echo $2|sed 's#\(^.*\)\[.*]#\1#g'`if[ "OK"!= "$COLOERTEXT"];thenecho -e "${BASE} [${COLOER} $COLOERTEXT ${RES}]"elseecho -e "${BASE} [${COLOER} $COLOERTEXT ${RES}]"fi}

The following is a color encoding list!

1. The red font abc is output, and the background color remains unchanged. The following three effects are the same:
Echo-e '\ 033 [0; 31; 1 m abc \ 033 [0m'
Echo-e "\ e [1; 31 m abc \ e [0 m"
Echo-e "\ e [0; 31; 1 m abc \ e [0 m"

2. The yellow font abc and red background color are displayed. The following three effects are the same:
Echo-e '\ 033 [41; 33; 1 m abc \ 033 [0m'
Echo-e "\ e [41; 33 m abc \ e [0 m"
Echo-e "\ e [41; 33; 1 m abc \ e [0 m"

30 Black 31 red, 32 green, 33 yellow, 34 blue, 35 purple, 36 green, 37 white (Gray)
Echo-e "\ e [1; 30 m skyapp exist \ e [0 m" // black
Echo-e "\ e [1; 31 m skyapp exist \ e [0 m" // red
Echo-e "\ e [1; 32 m skyapp exist \ e [0 m" // green
Echo-e "\ e [1; 33 m skyapp exist \ e [0 m" // yellow
Echo-e "\ e [1; 34 m skyapp exist \ e [0 m" // blue
Echo-e "\ e [1; 35 m skyapp exist \ e [0 m" // purple
Echo-e "\ e [1; 36 m skyapp exist \ e [0 m" // green
Echo-e "\ e [1; 37 m skyapp exist \ e [0 m" // white (Gray)

This article is from the "gccmx163.com" blog, please be sure to keep this source http://489381.blog.51cto.com/479381/1331709


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.