zoukankan      html  css  js  c++  java
  • 【shell】shell编程(六)-shell函数的应用

    linux shell 可以用户定义函数,然后在shell脚本中可以随便调用。

    shell中函数的定义格式如下:

    [ function ] funname [()]
    
    {
    
        action;
    
        [return int;]
    
    }

    说明:

    • 1、可以带function fun() 定义,也可以直接fun() 定义,不带任何参数。
    • 2、参数返回,可以显示加:return 返回,如果不加,将以最后一条命令运行结果,作为返回值。 return后跟数值n(0-255)

    例1:一个简单的例子:

    #!/bin/bash
    fun1(){
    echo "this is my first function"
    }
    echo "function start"
    fun1
    echo "function end"

    运行结果:

    $ ./test.sh
    function start
    this is my first function
    function end

    例2:一个带有return语句的函数:

    #!/bin/bash
    funWithReturn(){
        echo "这个函数会对输入的两个数字进行相加运算..."
        echo "输入第一个数字: "
        read aNum
        echo "输入第二个数字: "
        read anotherNum
        echo "两个数字分别为 $aNum 和 $anotherNum !"
        return $(($aNum+$anotherNum))
    }
    funWithReturn
    echo "输入的两个数字之和为 $? !"

    输出类似下面:

    这个函数会对输入的两个数字进行相加运算...
    输入第一个数字: 
    1
    输入第二个数字: 
    2
    两个数字分别为 12 !
    输入的两个数字之和为 3 !

    函数返回值在调用该函数后通过 $? 来获得。

    注意:所有函数在使用前必须定义。这意味着必须将函数放在脚本开始部分,直至shell解释器首次发现它时,才可以使用。调用函数仅使用其函数名即可。

    函数参数

    在Shell中,调用函数时可以向其传递参数。在函数体内部,通过 $n 的形式来获取参数的值,例如,$1表示第一个参数,$2表示第二个参数...

    带参数的函数示例:

    #!/bin/bash
    funWithReturn(){
            echo "this function have params"
            echo "the first para is $1"
            echo "the sec para is $2"
            echo "the hir para is $3"
            echo "the fourth para is $4"
    }
    
    funWithReturn 1 2 3 4

    结果:

    $ ./test.sh
    this function have params
    the first para is 1
    the sec para is 2
    the hir para is 3
    the fourth para is 4

      注意,$10 不能获取第十个参数,获取第十个参数需要${10}。当n>=10时,需要使用${n}来获取参数。

    参数处理说明
    $# 传递到脚本的参数个数
    $* 以一个单字符串显示所有向脚本传递的参数
    $$ 脚本运行的当前进程ID号
    $! 后台运行的最后一个进程的ID号
    $@ 与$*相同,但是使用时加引号,并在引号中返回每个参数。
    $- 显示Shell使用的当前选项,与set命令功能相同。
    $? 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。

    我们可以查看下面的例子:

    #!/bin/bash
    echo "$# is $#  "
    echo "$* is $*  "
    echo "$$ is $$  "
    echo "$@ is $@  "
    echo "$? is $?  "
    echo "$- is $-  "
    echo "$3 is $3  "
    echo "$2 is ${2}  "
    echo "$0 is $0  "
    echo "$0 is $(basename $0) "

    结果:

    $ ./test.sh 1 2 3 4 5
    $# is 5
    $* is 1 2 3 4 5
    $$ is 1288920
    $@ is 1 2 3 4 5
    $? is 0
    $- is hB
    $3 is 3
    $2 is 2
    $0 is ./test.sh
    $0 is test.sh

    附一个启动webmin的脚本

    #!/bin/bash
    #start webmin service
    start()
    {
    /etc/webmin/start >/dev/null 2> /dev/null
    if [ $? = '0' ] 
            then 
            echo "webmin is success start"
    fi
    }
    
    #stop webmin service
    stop()
    {
    /etc/webmin/stop > /dev/null 2> /dev/null
    if [ $? = '0' ] 
            then 
            echo "webmin is success stop"
    fi
    }
    
    #status webmin
    status()
    {
       /usr/bin/netstat -ano | /usr/bin/grep 10000 > /dev/null
       if [ $? != '0' ]
            then
            echo "webmin is not start"
       else
            echo "webmin is running"
        fi
    }
    
    ########read input##############
    str=$1
    if [ ${str} = 'start' ]
            then
            start
    elif [ ${str} = 'stop' ]
            then
            stop
    elif [ ${str} = 'status' ]
            then
            status
    else
            echo "please use {start,stop,status}"
    fi

    附一个启动tomcat的脚本

    #!/bin/bash
    #chkconfig: 2345 80 90
    #description:tomcat
    #input(start stop status) to operate tomcat service
    #start funciton(start tomcat service use /opt/apache-tomcat/apache-tomcat-7.0.72/bin/start.sh)
    export CATALINA_HOME=/opt/apache-tomcat/apache-tomcat-7.0.72
    start(){
            /usr/bin/sh "${CATALINA_HOME}"/bin/startup.sh
            if [ "$?" != "0" ]
            then 
                    echo "service is not success start"
            else
                    echo "service is success start"
    
                                                            fi
            exit 1
    }
    #stop function
    stop(){
            /usr/bin/sh "${CATALINA_HOME}"/bin/shutdown.sh
            if [ "$?" != "0" ]
            then 
                    echo "service is not success stop"
            else
                    echo "service is success stop"
    
                                                            fi
    }
    #status function
    status(){
            /usr/bin/ps -le | /usr/bin/grep java >/dev/null 2> /dev/null
            if [ "$?" != "0" ]
            then 
                    echo "service is not start"
            else
                    echo "service is running"
    
                                                    fi
    }
    #read input and dispose function
    input=${1}
    case ${input} in
    start)
            start
            ;;
    stop)
            stop
            ;;
    status)
            status
            ;;
    *)
            echo "please use {start to start tomcat,stop to stop tomcat,status to read tomcat status!}"
    esac
  • 相关阅读:
    HAOI2015 树上染色
    HAOI2010 软件安装
    T2 Func<in T1,out T2>(T1 arg)
    事无巨细
    LitJson JavaScriptSerializer
    数据库操作
    jQuery:总体掌握
    sql一个题的解法分析讲解
    Javascript系列:总体理解
    c#
  • 原文地址:https://www.cnblogs.com/qlqwjy/p/8683972.html
Copyright © 2011-2022 走看看