美文网首页
day66-shell编程之-function函数

day66-shell编程之-function函数

作者: 文娟_狼剩 | 来源:发表于2019-07-21 00:04 被阅读0次

    简单地说,函数的作用就是将程序里面多次被调用的相同代码组合起来(函数体),并为其取个名字(函数名)。其他所有想重复调用这部分代码的地方,只需要调用这个名字就好了。可以把函数独立的写到文件里,当需要调用函数时候,再加载进来使用。下面是使用shell函数的优势:

    • 把相同的程序段定义成函数,可以减少整个程序的代码量,提升开发效率
    • 增加程序的可读、易读性、提升管理效率
    • 可以实现程序功能模块化,使得程序具备通用性(可移植性)
    • 对于Shell来说,Linux系统里面近2000个命令都可以说是shell的函数。所以shell的函数还是很多的,这一点需要读者注意

    1、函数语法

    shell函数的常见语法格式,其标准写法为:

    function 函数名(){   \\推荐书写函数的方法(带括号)
    指令集...
    return n
    }
    

    简化写法1:

    function 函数名 {   \\不推荐使用此方法(无括号,函数名和左花括号之间需要有空格。)
    指令集...
    return n
    }
    

    简化写法2:

    函数名(){   \\不用function的方法
    指令集...
    return n
    }
    

    注:函数名的定义规则和变量是一样的:数字、字母、下划线,但是不能以 数字开头

    2、函数执行

    Shell函数分为最基本的函数和可以传参的函数两种,其执行方式分别说明如下:

    2.1 不带参数的函数

    不带参数的函数执行时,直接输入函数名即可(注意不带小括号)。格式如下:

    函数名

    有关执行函数的重要说明:

    • 执行Shell函数时候,函数名前面的function和函数后的小括号都不要带
    • 执行函数必须要在执行前定义或加载好(先定义再执行)
    • Shell执行系统中各种程序的顺序为:系统别名-->函数-->系统命令-->可执行文件
    • 在Shell里面,return功能与exit类似,作用是退出函数,而exit是退出脚本文件。
    • 如果函数存放于独立的文件中,被脚本加载使用时,需要使用source或者. 来加载。
    • 函数执行时,会和调用它的脚本共用变量,也可以为函数设定局部变量以及特殊位置参数
    • 在函数内一般使用local定义局部变量,这些变量离开后即消失。
      脚本的退出状态码,使用exit定义,之后脚本退出
      函数的退出状态码,使用return定义,之后函数退出
    2.2 带参数的函数

    带参数的函数执行方法,格式如下:

    函数名 参数1 参数2

    函数后接的参数说明:

    • Shell的位置函数(1、2...、#、*、?、@)都可以做作为函数的参数使用
    • 此时父脚本的参数临时的被函数参数所掩盖或隐藏
    • $0比较特殊,依然是父脚本的名称
      举例说明:
    [root@oldboy ~]# cat func1.sh 
    #!/bin/bash
    function hello(){
        echo "hello $1"
    }
    hello oldchang
    
    [root@oldboy ~]# sh func1.sh 
    hello oldchang
    

    练习:
    1>通过以上举例,思考,如何把参数通过执行脚本传递给函数?


    2>在函数里,打印出函数的参数个数、第一个参数、所有参数
    [root@shelledu /server/scripts]# cat test-func-1.sh 
    #!/bin/bash
    ##############################################################
    # File Name: test-func-1.sh
    # Version: v1.0
    # Author: wenjuan
    # date: 2019-07-18 10:07
    ##############################################################
    
    function print_info(){
        echo "脚本名称: $0"
        echo "参数个数: $#"
        echo "第一个参数: $1"
        echo "所有参数: $*"
    }
    print_info $*
    [root@shelledu /server/scripts]# sh test-func-1.sh my name is wwj
    脚本名称: test-func-1.sh
    参数个数: 4
    第一个参数: my
    所有参数: my name is wwj
    [root@shelledu /server/scripts]# 
    

    3、案例实操

    利用自己编译的nginx,写一个nginx的启动脚本,需求如下:
    1>类似于/etc/init.d/nginxd {start|stop|restart}
    2>使用systemctl实现开机自启动管理

    分析:
    # 编译nginx
    # 如果参数是start,则执行start的启动方式(/application/nginx/sbin/nginx)
    # 如果参数是stop,则执行stop的关闭方式(/application/nginx/sbin/nginx –s stop)
    # 如果stop的时候,没有进程,则输出一句话,"nginx 处于关闭状态"
    # 如果是restart,则先执行stop,再执行start

    [root@shelledu ~]# cat  /etc/init.d/nginxd 
    #!/bin/bash
    
    # 定义变量
    count=`netstat -lntup|grep nginx|wc -l`
    
    #定义函数
    function nginx_start(){
        if [ $count -gt 0 ];then
            echo 'nginx已经处于启动状态'
        else
            /application/nginx/sbin/nginx
            echo 'nginx启动成功'
        fi
    }
    function nginx_stop(){
        if [ $count -eq 0 ];then
            echo 'nginx已经处于关闭状态'
        else
            /application/nginx/sbin/nginx -s stop
            echo 'nginx已关闭'
        fi
    }
    # 如果参数是start,则执行start的启动方式(/application/nginx/sbin/nginx)
    if [ "$1" = "start" ];then
        nginx_start
    # 如果参数是stop,则执行stop的关闭方式(/application/nginx/sbin/nginx –s stop)
    # 如果stop的时候,没有进程,则输出一句话,"nginx 处于关闭状态"
    elif [ "$1" = "stop" ];then
        nginx_stop
    # 如果是restart,则先执行stop,再执行start
    elif [ "$1" = "restart" ];then
        nginx_stop
        sleep 2
        nginx_start
        echo 'nginx重启成功'
    else
        echo '参数错误,正确的参数是{start|stop|restart}'
    fi
    [root@shelledu ~]# cat /usr/lib/systemd/system/nginx_wenjuan.service
    [Unit]
    Description=nginx service
    After=nginx_wenjuan.target
    
    [Service]
    Type=forking
    ExecStart=/etc/init.d/nginxd start
    ExecReload=/etc/init.d/nginxd restart
    ExecStop=/etc/init.d/nginxd stop
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    
    [root@shelledu ~]# chmod +x  /etc/init.d/nginxd
    
    [root@shelledu /etc/init.d]# systemctl start nginx_wenjuan
    [root@shelledu ~]# systemctl status nginx_wenjuan.service 
    ● nginx_wenjuan.service - nginx service
       Loaded: loaded (/usr/lib/systemd/system/nginx_wenjuan.service; disabled; vendor preset: disabled)
       Active: active (running) since Thu 2019-07-18 11:01:13 CST; 48min ago
      Process: 13945 ExecStart=/etc/init.d/nginxd start (code=exited, status=0/SUCCESS)
     Main PID: 13951 (nginx)
       CGroup: /system.slice/nginx_wenjuan.service
               ├─13951 nginx: master process /application/nginx/sbin/nginx
               └─13952 nginx: worker process
    
    Jul 18 11:01:13 shelledu systemd[1]: Starting nginx service...
    Jul 18 11:01:13 shelledu nginxd[13945]: nginx启动成功
    Jul 18 11:01:13 shelledu systemd[1]: Started nginx service.
    [root@shelledu ~]#
    
    

    相关文章

      网友评论

          本文标题:day66-shell编程之-function函数

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