14-4shell脚本进阶函数实现

一、创建函数文件 . functions

RED="echo -e \E[1;31m"
GREEN="echo -e \E[1;32m"
END="\E[0m"
os_type(){
    if    grep -i -q ubuntu /etc/os-release;then
            echo ubuntu
    elif grep -i -q centos /etc/os-release;then
            echo centos
    else 
            echo "OS can not be supported!"
    fi
}


is_ipaddr(){
    [[ "$1" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$  ]] || { echo "$1 id not valid ip !";return 1; }
}

二、套用函数文件脚本

#!/bin/bash
. functions

if [ `os_type` = centos ];then
        yum install httpd -y
elif [ `os_type` = ubuntu ];then
        apt install apache2 -y
else
        echo "OS can not be supported!"
fi
${GREEN}"安装成功"$END

 

posted @ 2023-05-28 17:23  最拉的刺客  阅读(8)  评论(0)    收藏  举报