〖Linux〗Shell脚本修改输出文字颜色

时间:2022-12-19 05:40:35

Shell函数:

echocolor(){
color=${} && shift
case ${color} in
black)
echo -e "\e[0;30m${@}\e[0m"
;;
red)
echo -e "\e[0;31m${@}\e[0m"
;;
green)
echo -e "\e[0;32m${@}\e[0m"
;;
yellow)
echo -e "\e[0;33m${@}\e[0m"
;;
blue)
echo -e "\e[0;34m${@}\e[0m"
;;
purple)
echo -e "\e[0;35m${@}\e[0m"
;;
cyan)
echo -e "\e[0;36m${@}\e[0m"
;;
*)
echo -e "\e[0;37m${@}\e[0m"
;;
esac # --- end of case ---
}

使用示例:

cd $(dirname $(readlink -f ${}))
echocolor purple ">> 当前目录:${PWD}"
modname=$(grep 'obj-m' Makefile | awk '{print $3}') && modname=${modname/.o}
echocolor purple ">> 模块名称:${modname}"
echocolor yellow ">> 卸载旧的模块.."
adb shell rmmod ${modname}
echocolor yellow ">> 正在执行编译.."
make
echocolor yellow ">> 正在上传文件.."
adb push k860_words_count.ko /tmp/
echocolor yellow ">> 正在安装模块.."
adb shell insmod /tmp/k860_words_count.ko
echocolor cyan ">> 输出调试信息.."
adb shell cat /proc/kmsg | grep word

截图展示:

〖Linux〗Shell脚本修改输出文字颜色

参考文献:

  [1] http://*.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux