#!/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 - 如何把一個指令碼註冊為命令,成為系統的新命令