shell学习笔记之八(函数)

来源:互联网 发布:清华学云计算什么专业 编辑:程序博客网 时间:2024/05/23 01:38
函数    定义:        1、无返回值            #function为关键字,FUNCTION_NAME为函数名            function FUNCTION_NAME(){                command1                command2                ...            }            省略关键字function,效果一样            FUNCTION_NAME(){                command                ....            }            例:                1、简单函数声明和调用                    #!/bin/bash                    function sayHello(){                        echo "Hello World!"                    }                    sayHello                    注意:                        1、sayHello调用的时候没有(),sayHello()这样的调用会出错。                        2、如果先调用再声明,则调用和声明之间不能有其他语句                2、计算文件的行数                    #!/bin/bash                    if [[ $# -lt 1 ]];then                        echo "Please input a filename."                        return                    fi                    file=$1                    countLine                    function countLine(){                        local line=0                        while read                        do                            let "line++";                        done < $file                        echo "$file has $line lines."                    }        2、函数的返回值            return            获取返回值的主要方式是$?            例:                #检测文件是否存在                #!/bin/bash                file=$1                check                if [ $? -eq 0 ];then                    echo "$file exists."                else                    echo "$file doesn't exist."                fi                function check(){                    if [ -e $file ];then                        return 0                    else                        return 1                    fi                }        3、带参数的函数            1、位置参数                这个和高级语言不一样,在函数声明里没有指定参数,而是直接在函数体里使用,调用的时候直接传入参数即可                例:                    1、检测文件是否存在                        #!/bin/bash                        check $1 #这里不再使用file变量                        if [ $? -eq 0 ];then                            echo "$1 exists."                        else                            echo "$1 doesn't exist."                        fi                        function check(){                            if [ -e $1 ];then #函数体里的参数                                return 0                            else                                return 1                            fi                        }                    2、计算两数之和                        #!/bin/bash                        function add(){                            local tmp=0                            i=$1                            j=$2                            let "tmp=i+j"                            return $tmp                        }                        add $1 $2                        echo "$?"            2、指定位置参数值               使用set内置命令给脚本指定位置参数的值(又叫重置)。一旦使用set设置了传入参数的值,脚本将忽略运行时传入的位置参数(实际上是被set            重置了位置参数的值)                  例:                    #!/bin/bash                    set 1 2 3 4                     count=1                    for var in $@                    do                        echo "Here \$$count is $var"                        let "count++"                    done                    注意:输入时不管有多少参数都重置为四个参数。                        如:                            . ./function03.sh a b c d e f                        结果:                            Here $1 is 1                            Here $2 is 2                            Here $3 is 3                            Here $4 is 4                    注意:有什么意义?            3、移动位置参数                回顾:                    shift,在不加任何参数的情况下,这个命令可以将位置参数向左移动一位。                    例:                        #!/bin/bash                        until [ $# -eq 0 ]                        do                            echo "Now \$1 is:$1,total paramert is:$#"                            shift                        done                        注意:活用位置参数,                            $@/$*:所有参数                            $1..$n:第n个参数,当n大于10时,要将n用()括起来                            $0:脚本本身                                当用‘.’执行脚本时为bash                                当用bash执行脚本时返回的文件名                                当用./scriptname执行时返回./scriptname                            $#:所有参数                扩展                    指定左移的位数,shift n                    例:                        #!/bin/bash                        echo "$0"                        until [ $# -eq 0 ]                        do                            echo "Now \$1 is:$1,total paramert is:$#"                            shift 2                        done                        注意:如果输入命令时指定了奇数个参数,则脚本会陷入死循环。                           4、函数库            为了和普通函数区别,在实践中,建议库函数使用下划线(_)开头            加载库函数:                1、使用"点"(.)命令                2、使用source命令                例:                    1、建立函数库                        实际上就是写一个都是函数的脚本文件                        例:建立库lib01.sh                            function _checkFileExist(){                                if [ -e $1 ];then                                    echo "File:$1 exists"                                else                                    echo "File:$1 doesn't exists"                                fi                            }                    2、使用                        #!/bin/bash                        #source ./lib01.sh                        . ./lib01.sh                        _checkFileExist $1            系统函数库:                /etc/init.d/functions(我的系统是ubuntu,没看到这个文件)                一个27个函数
0 0
原创粉丝点击