zoukankan      html  css  js  c++  java
  • shell编程之函数

    一、函数定义和调用

    函数是Shell脚本中自定义的一系列执行命令,一般来说函数应该设置有返回值(正确返回0,错误返回非0)。对于错误返回,可以定义其他非0正值来细化错误。使用函数最大的好处是可避免出现大量重复代码,同时增强了脚本的可读性:如果你在某个Shell脚本中看到checkFileExist的这样的代码(实际是函数调用),一定不难猜出代码的作用。

    [root@Cfhost-170820-UCNK ~]# cat sayHello.sh
    #!/bin/bash
    function sayHello(){  #关键字function 可以省略

    echo "Hello"

    }
    echo "Call function sayHello" #提示函数调用
    sayHello #函数调用

    [root@Cfhost-170820-UCNK ~]# sh sayHello.sh
    Call function sayHello
    Hello

    [root@Cfhost-170820-UCNK ~]# cat countLine.sh
    #!/bin/bash
    FILE=/etc/passwd
    function countLine(){
    local i=0
    while read line
    do
    let ++i
    done < $FILE
    echo "$FILE have $i line"
    }
    echo "Call functino countLine"
    countLine

    [root@Cfhost-170820-UCNK ~]# sh countLine.sh
    Call functino countLine
    /etc/passwd have 25 line

    二、函数的返回值

    函数的返回值又叫函数的退出状态,实际上是一种通信方式。Shell中的函数可以使用"返回值"的方式来给调用者反馈信息(使用return关键字),不要忘了获取上一个命令返回值的方式是使用$? 这样获取函数返回值的主要方式。

    [root@Cfhost-170820-UCNK ~]# cat checkFileExist.sh
    #!/bin/bash
    FILE=/etc/notExistFile  #定义一个不存在的文件
    function checkFileExist(){ #定义checkFileExist函数
    if [ -f $FILE ];then
    return 0
    else
    return 1
    fi
    }
    checkFileExist  #调用函数

    if [ $? -eq 0 ];then
    echo "$FILE exist"
    else
    echo "$FILE not exist"
    fi

    [root@Cfhost-170820-UCNK ~]# sh checkFileExist.sh
    /etc/notExistFile not exist

    三、带参数的函数

    checkFileExist.sh脚本中定义了checkFileExist函数,但是可以看到这个脚本实际上写死了FILE变量,这会造成想要判断不同的文件是否存在时,需要修改脚本中的FILE变量 -也就是要对代码本身的内容进行修改,这也是典型的代码和数据没有分开而导致的问题。事实上,可以通过定义带参数的函数解决这个问题。

    [root@Cfhost-170820-UCNK ~]# cat checkFileExist_v2.sh
    #!/bin/bash
    function checkFileExist(){
    if [ -f $1 ];then
    return 0
    else
    return 1
    fi


    }


    echo "Call function checkFileExist"
    checkFileExist $1

    if [ $? -eq 0 ];then
    echo "$1 exist"
    else
    echo "$1 not exist"
    fi

    执行结果:

    [root@Cfhost-170820-UCNK ~]# sh checkFileExist_v2.sh /etc/not
    Call function checkFileExist
    /etc/not not exist

    四、指定位置参数值

     除了在脚本运行时给脚本传入的位置参数外,还可以使用内置命令set命令给脚本指定位置参数的值(又叫重置)。一旦使用set设置了传入参数的值,脚本将忽略运行时传入的位置参数(实际上被set命令重置了位置参数的值)

    #!/bin/bash
    set 1 2 3 4 5 6
    COUNT=1
    for i in $@
    do
    echo "Here $ $COUNT is $i"
    let "COUNT++"
    done

    执行结果如下:

    [root@Cfhost-170820-UCNK ~]# sh set01.sh a b c d e f
    Here $ 1 is 1
    Here $ 2 is 2
    Here $ 3 is 3
    Here $ 4 is 4
    Here $ 5 is 5
    Here $ 6 is 6

    五、移动位置参数

    在shell中使用shift命令移动位置参数,shift命令可让位置参数左边移动一位

    [root@Cfhost-170820-UCNK ~]# cat shift_03.sh
    #!/bin/bash

    until [ $# -eq 0 ]
    do
    echo "Now $1 is: $1, total parameter is:$#"
    shift
    done

    执行结果:

    [root@Cfhost-170820-UCNK ~]# sh shift_03.sh a b c
    Now $1 is: a, total parameter is:3
    Now $1 is: b, total parameter is:2
    Now $1 is: c, total parameter is:1

    利用shift计算脚本中所有参数的和:

    [root@Cfhost-170820-UCNK ~]# cat shift_04.sh
    #!/bin/bash
    TOTAL=0
    until [ $# -eq 0 ]
    do
    let "TOTAL=TOTAL+$1"
    shift
    done
    echo $TOTAL

    执行结果:

    [root@Cfhost-170820-UCNK ~]# sh shift_04.sh 10 20 30
    60

  • 相关阅读:
    hdu6229 Wandering Robots 2017沈阳区域赛M题 思维加map
    hdu6223 Infinite Fraction Path 2017沈阳区域赛G题 bfs加剪枝(好题)
    hdu6438 Buy and Resell 买卖物品 ccpc网络赛 贪心
    hdu6441 Find Integer 求勾股数 费马大定理
    bzoj 1176 Mokia
    luogu 3415 祭坛
    bzoj 1010 玩具装箱
    bzoj 3312 No Change
    luogu 3383【模板】线性筛素数
    bzoj 1067 降雨量
  • 原文地址:https://www.cnblogs.com/youcong/p/7944903.html
Copyright © 2011-2022 走看看