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 好买网

你可能感兴趣的:(前端)