美文网首页
shell-12 函数 nginx脚本

shell-12 函数 nginx脚本

作者: 巴巴11 | 来源:发表于2020-05-10 12:15 被阅读0次

    函数的语法

    语法一:
    
    函数名 () {
        代码块
        return N
        }
    
    
    语法二:
    function 函数名 {
          代码块
          return N
          }
    

    函数的应用
    定义一个函数

    print () {
        echo "welcome to ayitula"
    }
    或者
    
    function hello {
        echo "hello world"
    }
    

    hello就是函数名
    print 和 hello就是函数的名字,函数名字命名参考变量一节中的变量命名规则

    函数调用

    定义好函数后,如果想调用该函数,只需通过函数名调用即可。

    函数演示案例

    #!/bin/bash
    
    N1 () {
    echo "`date +%F`"
    }
    
    N2 () {
    echo -e "\t\t\t\twelcome to ayitula"
    echo -e "\n"
    }
    
    N3 () {
    echo "1) 剪子"
    echo "2) 石头"
    echo "3) 布"
    }
    
    N4 () {
    echo -e "\n\n\n"
    read -p "请输入代码: " DL
    }
    
    #方便调整代码执行顺序
    N2
    #代码重复调用
    N1
    N1
    N3
    N4
    
    
    输出
    
    [root@www ~]# sh x2
                    welcome to ayitula
    
    
    2019-02-19
    2019-02-19
    1) 剪子
    2) 石头
    3) 布
    
    
    
    
    请输入代码: 1
    
    #!/bin/bash
    #nginx service manager scripts
    
    #==============================variables
    nginxdoc="/usr/local/nginx"
    nginxd="$nginxdoc/sbin/nginx"
    pid="$nginxdoc/logs/nginx.pid"
    conf="$nginxdoc/conf/nginx.conf"
    
    
    #==============================function
    mystart () {
        if [ -f $pid] && pstree -p |grep `cat $pid` &>/dev/null;then
            echo "nginx already run..."
            exit 0
        else
            if $nginxd;then
                echo -e "nginx start\t\t\t\t\t\t[\033[32m ok \033[0m]"
            else
                echo -e "nginx start\t\t\t\t\t\t[\033[31m fail \033[0m]"
            fi
        fi        
    }
    
    
    mystop () {
        if [ -f $pid ] && pstree -p |grep `cat $pid` &>/dev/null;then
            if killall -s QUIT $nginxd;then
                echo -e "nginx stop\t\t\t\t\t\t[\033[32m ok \033[0m]"
            else
                echo -e "nginx stop\t\t\t\t\t\t[\033[31m fail \033[0m]"
            fi
        else
            echo "nginx already stop..."
        fi              
    }
    
    myrestart () {
        mystop;sleep 2; mystart
    }
    
    myreload () {
        if [ -f $pid ] && pstree -p |grep `cat $pid` &>/dev/null;then
            if killall -s HUP $nginxd;then
                echo -e "nginx reload\t\t\t\t\t\t[\033[32m ok \033[0m]"
            else
                echo -e "nginx reload\t\t\t\t\t\t[\033[31m fail \033[0m]"
            fi
        else
            echo "nginx is stop..."
        fi    
    }
    
    mystatus () {
        if [ -f $pid ] && pstree -p |grep `cat $pid` &>/dev/null;then
            echo "nginx is open"
        else
            echo "nginx is stop"
        fi        
    }
    
    #==============================main
    #called function
    
    case $1 in
    start|START)
        mystart
    ;;
    stop) mystop;;
    restart) myrestart;;
    reload) myreload;;
    status) mystatus;;
    esac
    
    

    相关文章

      网友评论

          本文标题:shell-12 函数 nginx脚本

          本文链接:https://www.haomeiwen.com/subject/ksxjnhtx.html