SheLL#System function and echo color

Shell programming call system function library and call color function library

Call the system's own function library in your own shell script

Thinking method:

1. Use [[ ]], &&, || symbols to replace the if condition judgment, which is suitable for situations where if judgment is needed in a large if
2. The echo output color script uses position variables

Simple to use, command line introduction

The /etc/init.d/functins file is a function library that comes with the system. When we write a shell script, we can get four echo related commands from source /etc/init.d/functions. Using these commands can make our own The script output is the same as that of the system. First perform the demonstration on the command line. After source, enter echo to complete the tab. You can see that we get the command in the system output format in four cases of failure passed success warning.

[root@sun init.d]# source functions 
[root@sun init.d]# echo
echo   echo_failure  echo_passed   echo_success  echo_warning  
学会简单的用法,直接这样写
[root@sun init.d]# failure && echo "失败情况的系统输出"
失败情况的系统输出                                         [失败]
[root@sun init.d]# passed && echo "失败情况的系统输出"
失败情况的系统输出                                         [通过]
[root@sun init.d]# warning && echo "警告情况的系统输出"
警告情况的系统输出                                         [警告]
[root@sun init.d]# success && echo "成功情况的系统输出"
成功情况的系统输出                                         [  确定  ]

The following is the color effect performed
Insert picture description here

Simple to use, shell script example

#!/bin/bash
# Author:kakaops
# Email:[email protected]

# 示例success和failure在启动和停止服务中的应用
# 以nginx为例子,nginx的官方启动命令为nginx,官方停止命令为nginx -s stop

# 示例如何用&&和||替换需要多行的if判断

source /etc/init.d/functions
###################
# start函数
###################
start_nginx(){
    
    
    [[ $(netstat -auntpl |grep nginx|wc -l) > 0 ]] && failure && echo "Nginx is already running" && exit
    nginx
    ([ $? -eq 0 ] && success && echo "Nginx start sucessfully!") ||( failure && echo "Failed start nginx" ) 
}

###################
# stop函数
###################
stop_nginx(){
    
    
    [[ $(ss -auntpl |grep nginx |wc -l) == 0 ]] && failure && echo "Nginx is not running" && exit
    nginx -s stop
    ([ $? -eq 0 ] && success && echo "Nginx stop sucessfully!") || ( failure && echo "Failed stop nginx" )
}

case $1 in
    start)
        start_nginx
        ;;
    stop)
        stop_nginx
        ;;
    *)
        exit
        ;;
esac

Homemade echo color.sh

Echo changes the color of the current terminal (\e can also be \033)
echo -e color (-e is the activation escape character)
[root@sun ~]# echo -e "\e[1;31mThis is red text\e [0m"
Use the echo command to print colored text:
text color:
echo -e "\e[1;31mThis is red text\e[0m"
\e[1;31m Set the color to red
\e[0m will The color is reset
. The color of the font itself and the background color of the text are not required for the color code of the text action around the semicolon, and the quotation marks can be used in multiple formats

Font foreground color:
color code: reset=0, black=30, red=31, green=32, yellow=33, blue=34, magenta=35, cyan=36, white=37
text background color:
echo- e "\e[1;42mGreed Background\e[0m"
color code: reset=0, black=40, red=41, green=42, yellow=43, blue=44, magenta=45, cyan=46 , White=47
text action:
echo -e "\e[37;31;5mMySQL Server Stop...\e[0m"
action code: 0 close all attributes, 1 set high brightness (bold), 3 tilt, 4 underline, 5 flashing, 7 reverse display, 8 blanking

The output color of python print() is the same as echo: -e is replaced by \033
Insert picture description here

Usage of color.sh

In this script, just set the color to highlight, other parameters, if necessary, add them by yourself, the important thing is to change the way of thinking

# 处理单行命令的颜色输出
function black(){
    
    
    echo -e "\e[1;30m$1\e[0m"
}
function red(){
    
    
    echo -e "\e[1;31m$1\e[0m"
}
function green(){
    
    
    echo -e "\e[1;32m$1\e[0m"
}
function yellow(){
    
    
    echo -e "\e[1;33m$1\e[0m"
}
function blue(){
    
    
    echo -e "\e[1;34m$1\e[0m"
}
function carmine(){
    
    
    echo -e "\e[1;35m$1\e[0m"
}
function cyan(){
    
    
    echo -e "\e[1;36m$1\e[0m"
}
function white(){
    
    
    echo -e "\e[1;37m$1\e[0m"
}

First execute it in the current shell to see the effect and then make improvements
Insert picture description here

Can print out a file in color

Insert picture description here
Color print the output of a command
Insert picture description here

The above verification, our color.sh script can work, but the script is very simple but very long. Improve it

color.sh defines associative array to simplify script

function color(){
    
    
declare -A color_info
color_info=([black]=30 [red]=31 [green]=32 [yellow]=33 [blue]=34 [carmine]=35 [cyan]=36 [white]=37)
echo -e "\e[1;${color_info[$1]}m$2\e[0m"
}

There is a little difference between the call and the above, you need to pass two positional parameters $1, $2
command line test:

Insert picture description here

Output file test
Insert picture description here

Command output test
Insert picture description here

Simple to use, script example

Cooperate with cute little cows cowsay and
other animal catalogs
/usr/share/cowsay
cowsay -f kiss " cheating father"
-f specify other animals
cowsay " (colorred" (color red "(colorred"(ip a)”)"
cowsay -f kiss ( c o l o r r e d " (color red " (colorred"(ip a)")

#!/bin/bash
# Author:kakaops
# Email:[email protected]

# 示例success和failure在启动和停止服务中的应用
# 以nginx为例子,nginx的官方启动命令为nginx,官方停止命令为nginx -s stop

# 示例如何用&&和||替换需要多行的if判断

source /etc/init.d/functions
source ./color.sh

# 如果没有奶牛,就安装一下
#yum -y install cowsay

###################
# start函数
###################
start_nginx(){
    
    
    [[ $(netstat -auntpl |grep nginx|wc -l) > 0 ]] && cowsay $(color red "Nginx is already running") && exit
    nginx
    ([ $? -eq 0 ] && cowsay -f turtle $(color green "Nginx start sucessfully!")) ||( cowsay $(color red "Failed start nginx" )) 
}

###################
# stop函数
###################
stop_nginx(){
    
    
    [[ $(ss -auntpl |grep nginx |wc -l) == 0 ]] && cowsay -f tux $(color red "Nginx is not running") && exit
    nginx -s stop
    ([ $? -eq 0 ] && cowsay -f kitty $(color green "Nginx stop sucessfully!")) || (cowsay $(color red "Failed stop nginx" ))
}
case $1 in
    start)
        start_nginx
        ;;
    stop)
        stop_nginx
        ;;
    *)
        exit
        ;;
esac

Insert picture description here
Insert picture description here
Insert picture description here

Text foreground color, text background color, text action

Define multiple associative array text foreground colors, background colors, and actions can all be output. Four positional parameters are required.
$1 specifies the text foreground color
$2 specifies the text background color
$3 specifies the text action
$4 specifies the output content

#!/bin/bash
# Author:kakaops
# Email:[email protected]

function color-plus(){
    
    
declare -A color_info
color_info=([black]=30 [red]=31 [green]=32 [yellow]=33 [blue]=34 [carmine]=35 [cyan]=36 [white]=37)
declare -A color_info_back
color_info_back=([black]=40 [red]=41 [green]=42 [yellow]=43 [blue]=44 [carmine]=45 [cyan]=46 [white]=47)
declare -A action_info
action_info=([bold]=1 [tilt]=3 [underline]=4 [twinkle]=5 [invert]=7 [blank]=8)

echo -e "\e[${color_info[$1]};${color_info_back[$2]};${action_info[$3]}m$4\e[0m"
}

#加粗:bold
#倾斜:tilt
#下划线:underline
#闪烁:twinkle
#反显:invert
#消隐:blank

Insert picture description here
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/kakaops_qing/article/details/108987405