美文网首页
shell系列5-函数

shell系列5-函数

作者: 只是甲 | 来源:发表于2022-04-14 12:10 被阅读0次

    一. 函数概述

    语法:

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

    必须在调用函数地方之前,先声明函数,shell脚本是逐行运行。不会像其它语言一样先编译。

    函数返回值,只能通过$?系统变量获得,可以显示加:return返回,如果不加,将以最后一条命令运行结果,作为返回值。return后跟数值n(0-255)

    二. 函数实例

    2.1 函数实例1

    #!/bin/bash
    
    hello()
    {
    echo "Hello, today is `date`"
    }
    
    echo "Now going to run function hello()"
    hello
    
    echo "end the function hello()"
    

    运行记录:

    [root@hp5 tmp]# sh func1.sh 
    Now going to run function hello()
    Hello, today is 2022年 03月 22日 星期二 15:53:11 CST
    end the function hello()
    

    2.2 函数实例2

    写个函数统计当前目录下有多少文件和目录

    #!/bin/bash
    
    directory()
    {
        let "filenum = 0"
        let "dirnum = 0"
       
        ls
        echo ""
       
        for file in $( ls )
        do
            if [ -d $file ]
            then
                let "dirnum = dirnum + 1"
            else
               let "filenum = filenum + 1"
            fi
        done
    
        echo "The number of directory is $dirnum"
        echo "The number of file is $filenum"
    }
    
    directory
    

    测试记录:

    [root@hp5 tmp]# sh func2.sh 
    dir1  dir2  dir3  dir4  func1.sh  func2.sh  ifelifelse_exam1.sh
    
    The number of directory is 4
    The number of file is 3
    

    2.3 函数实例3--向函数传参

    #!/bin/bash
    
    half()
    {
    let "n = $1"
    let "n = n/2"
    echo "In function half() n is $n"
    }
    
    let "m = $1"
    echo "Before the function half() is called, m is $m"
    half $m
    echo "After the function half() is called, m is $m"
    

    测试记录:

    [root@hp5 tmp]# sh func3.sh 10
    Before the function half() is called, m is 10
    In function half() n is 5
    After the function half() is called, m is 10
    

    2.4 函数实例4--函数返回值

    有时需要脚本执行完成后返回特定的值来完成脚本的后继操作,这些特定的值就是函数返回值。在Linux Shell编程中,函数通过return返回其退出状态,0表示无错误,1表示有错误。在脚本中可以有选择的使用return语句,因为函数在执行完最后一条语句后将执行调用该函数的地方执行后继操作。

    #!/bin/bash
    
    
    show_week()
    {
    echo -n "What you input is: "
    echo "$1"
    
    case $1 in
    0)
    echo "Today is Sunday. "
    return 0;;
    1)
    echo "Today is Monday. "
    return 0;;
    
    2)
    echo "Today is Tuesday. "
    return 0;;
    
    3)
    echo "Today is Wednesday. "
    return 0;;
    
    4)
    echo "Today is Thursday. "
    return 0;;
    
    5)
    echo "Today is Friday. "
    return 0;;
    
    6)
    echo "Today is Saturday. "
    return 0;;
    *)
    return 1;;
    esac
    }
    
    
    if show_week "$1"
    then
    echo "What you input is right! "
    else
    echo "What you input is wrong! "
    fi
    
    exit 0
    

    测试记录:

    [root@hp5 tmp]# sh func4.sh 0
    What you input is: 0
    Today is Sunday. 
    What you input is right! 
    [root@hp5 tmp]# sh func4.sh 8
    What you input is: 8
    What you input is wrong! 
    [root@hp5 tmp]# 
    

    函数脚本中exit 0 就是退出整个函数

    2.5 函数实例5--函数间相互调用

    #!/bin/bash
    
    square()
    {
    echo "Please input the num: "
    read num1
    
    let "squ=num1 * num1"
    
    echo "Square of $num1 is $squ. "
    }
    
    cube()
    {
    echo "Please input the num: "
    read num2
    
    let "c=num2 * num2 * num2"
    
    echo "Cube of $num2 is $c. "
    }
    
    power()
    {
    echo "Please input the num: "
    read num3
    echo "Please input the power: "
    read p
    
    let "temp = 1"
    for (( i=1; i <= $p; i++ ))
    do
    let "temp=temp*num3"
    done
    echo "power $p of $num3 is $temp. "
    }
    
    choice()
    {
    echo "Please input the choice of operate(s for square; c for cube and p for power): "
    read char
    
    case $char in
    s)
    square;;
    c)
    cube;;
    p)
    power;;
    *)
    echo "What you input is wrong! ";;
    esac
    }
    
    choice
    

    测试记录:

    [root@hp5 tmp]# sh func5.sh 
    Please input the choice of operate(s for square; c for cube and p for power): 
    s
    Please input the num: 
    10
    Square of 10 is 100. 
    

    2.6 函数实例6--函数递归调用

    Linux Shell中可以递归调用函数,即函数可以直接或间接调用其自身。在递归调用中,主调函数又是被调函数。执行递归函数将反复调用其自身,每调用一次就进入新的一层。

    #!/bin/bash
    
    fact ()
    {
    local num=$1
    
    if [ "$num" -eq 0 ]
    then
    factorial=1
    else
    let "decnum=num-1"
    
    fact $decnum
    
    let "factorial=$num * $?"
    fi
    return $factorial
    }
    
    fact $1
    echo "Factorial of $1 is $?"
    
    exit 0
    

    测试记录:

    [root@hp5 tmp]# sh func6.sh 1
    Factorial of 1 is 1
    [root@hp5 tmp]# sh func6.sh 2
    Factorial of 2 is 2
    [root@hp5 tmp]# sh func6.sh 3
    Factorial of 3 is 6
    [root@hp5 tmp]# sh func6.sh 4
    Factorial of 4 is 24
    [root@hp5 tmp]# sh func6.sh 5
    Factorial of 5 is 120
    [root@hp5 tmp]# sh func6.sh 6
    Factorial of 6 is 208
    [root@hp5 tmp]# 
    

    为了观察递归调用的工作过程,下面跟踪下面语句的执行num=3,下面是递归的执行过程:
    num=3:发现num的值不等于0,所以调用函数 fact 3
    num=2:发现num的值不等于0,所以调用函数fact 2
    num=1:发现num的值不等于0,所以调用函数fact 1
    num=0,这是num是等于0的,所以返回调用fact 0,返回factorial的值为1
    num=1,返回factorial的值为1*1=1
    num=2,返回factorial的值为1*2=2
    num=3,返回factorial的值为1*2*3=6
    在最终传递到0时,fact函数开始将先前的调用逐个分解,直到num=3的原始调用为止,并返回最终结果为6。

    相关文章

      网友评论

          本文标题:shell系列5-函数

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