乐趣区

关于前端:shell定义一个颜色输出字符串函数

写法 1

#!/bin/bash
function echo_color(){if [ $1 == "green"]; then
        echo -e "\033[32;40m$2\033[0m"
    elif [$1 == "red"]; then
        echo -e "\033[31;40m$2\033[0m"
    fi
}
echo_color green "我是绿色"
echo_color red "我是红色"

写法 2
也能够在后面不写 function,也能够定义函数

#!/bin/bash
echo_color(){
case $1 in
green)
    echo -e "\033[32;40m$2\033[0m"
;;
red)
    echo -e "\033[31;40m$2\033[0m"
;;
*)
    echo "Example: echo_color red string"
esac
}
echo_color green "我是绿色"

更多建站及源码交易信息请见 GoodMai 好买网

退出移动版