Linux Shell CentOS 写一个输出命令 支持颜色
1、代码
#!/usr/bin/env bash
# ---------------------------------------------
# 使用说明
# 格式 prt "hello world" red 0
# 格式 prt -h 打开帮助
# ---------------------------------------------
# 输出的消息
MSG=$1
# 指定颜色
COLOR=$2
# 是否开启背景色,1开启,0 不开始
BG=$3
# 改变字体显示模式为高亮模式
echo -n -e "\033[31m\033[1m"
if [ -z "$MSG" ]; then
echo -e "\033[31m 参数不能为空 \033[1m"
echo -e "\033[31m see prt -h \033[1m"
# 恢复下一行的颜色为正常模式
echo -n -e "\033[31m\033[0m"
exit 1
fi
if [ "$MSG" == "-h" ]; then
echo -e "\033[32m prt [message][color:-green][background:-0] \033[1m"
echo -e "\033[32m red = 红色 \033[1m"
echo -e "\033[32m green = 绿色 \033[1m"
echo -e "\033[32m yellow = 黄色 \033[1m"
echo -e "\033[32m blue = 蓝色 \033[1m"
echo -e "\033[32m purple = 紫色 \033[1m"
echo -e "\033[32m skyblue = 天蓝色 \033[1m"
# 恢复下一行的颜色为正常模式
echo -n -e "\033[31m\033[0m"
exit 1
fi
if [ -z "$COLOR" ]; then
COLOR=green
fi
if [ -z "$BG" ]; then
BG=0
fi
MSG="$MSG"
case $COLOR in
"red")
if [ $BG == 1 ]; then
echo -e "\033[41;37m $MSG \033[1m"
else
echo -e "\033[31m $MSG \033[1m"
fi
;;
"green")
if [ $BG == 1 ]; then
echo -e "\033[42;37m $MSG \033[1m"
else
echo -e "\033[32m $MSG \033[1m"
fi
;;
"yellow")
if [ $BG == 1 ]; then
echo -e "\033[43;37m $MSG \033[1m"
else
echo -e "\033[33m $MSG \033[1m"
fi
;;
"blue")
if [ $BG == 1 ]; then
echo -e "\033[44;37m $MSG \033[1m"
else
echo -e "\033[34m $MSG \033[1m"
fi
;;
"purple")
if [ $BG == 1 ]; then
echo -e "\033[45;37m $MSG \033[1m"
else
echo -e "\033[35m $MSG \033[1m"
fi
;;
"skyblue")
if [ $BG == 1 ]; then
echo -e "\033[46;37m $MSG \033[1m"
else
echo -e "\033[36m $MSG \033[1m"
fi
;;
*)
echo -e "\033[31m see prt -h \033[1m"
;;
esac
# 恢复下一行的颜色为正常模式
echo -n -e "\033[31m\033[0m"
注册脚本为命令参考 Linux - 如何把一个脚本注册为命令,成为系统的新命令
2、演示
转载:https://blog.csdn.net/qq_15071263/article/details/109198050
查看评论