SheLL#系统函数和echo颜色

shell编程调用系统函数库和调用颜色函数库

在自己的shell脚本中调用系统自带函数库

思想方法:

1、利用[[ ]]、&&、 ||符号进行对if条件判断进行替换,适合在一个大的if里面还需要进行if判断的情况
2、echo输出颜色脚本利用位置变量

简单使用,命令行介绍

/etc/init.d/functins文件是系统自带的函数库,我们在写shell脚本的时候,source /etc/init.d/functions就可以得到四个echo相关命令,使用这些命令可以让自己的脚本输出和系统的一样,先在命令行进行演示,source之后,输入echo进行tab补全,可以看到我们得到了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 "成功情况的系统输出"
成功情况的系统输出                                         [  确定  ]

下面的是执行的有颜色的效果
在这里插入图片描述

简单使用,shell脚本示例

#!/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

自制echo的color.sh

echo改变当前终端的颜色(\e也可以是\033)
echo -e 颜色(-e是激活转义字符)
[root@sun ~]# echo -e “\e[1;31mThis is red text\e[0m”
用echo命令打印带有色彩的文字:
文字色:
echo -e “\e[1;31mThis is red text\e[0m”
\e[1;31m 将颜色设置为红色
\e[0m 将颜色重新置回
字体本身的颜色和文字背景色文字动作色码在分号左右没有要求,引号可以并列多个格式

字体前景色:
颜色码:重置=0,黑色=30,红色=31,绿色=32,黄色=33,蓝色=34,洋红=35,青色=36,白色=37
文字背景色:
echo -e “\e[1;42mGreed Background\e[0m”
颜色码:重置=0,黑色=40,红色=41,绿色=42,黄色=43,蓝色=44,洋红=45,青色=46,白色=47
文字动作:
echo -e “\e[37;31;5mMySQL Server Stop…\e[0m”
动作码:0 关闭所有属性、1 设置高亮度(加粗)、3 倾斜 、4 下划线、5 闪烁、7 反显、8 消隐

python print() 输出颜色和echo一样:-e换成 \033
在这里插入图片描述

color.sh的用法

在这个脚本里,只是设置颜色为高亮显示,别的参数,如果需要,自行添加,重要的是变思想方法

# 处理单行命令的颜色输出
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"
}

先在当shell执行一下,看看效果然后进行改进
在这里插入图片描述

可以用颜色打印输出一个文件

在这里插入图片描述
颜色打印一条命令的输出
在这里插入图片描述

上述验证,我们的color.sh脚本是可以行的,但脚本虽然很简单但是很长,改进一下

color.sh定义关联数组简化脚本

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"
}

调用的时候和上面有点区别,需要传递两个位置参数 $1、$2
命令行测试:

在这里插入图片描述

输出文件测试
在这里插入图片描述

命令输出测试
在这里插入图片描述

简单使用,脚本示例

配合可爱的小奶牛cowsay
其他的动物目录
/usr/share/cowsay
cowsay -f kiss “坑爹啊”
-f 指定其他动物
cowsay “ ( c o l o r r e d " (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

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

文字前景色,文字背景色,文字动作

定义多个关联数组文字前景色、背景色、动作都可以输出,需要四个位置参数
$1  指定文字前景色
$2  指定文字背景色
$3  指定文字动作
$4  指定输出内容

#!/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

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/kakaops_qing/article/details/108987405