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

  • 相关阅读:
    在vs2010里使用EF4.3的Code First个人使用笔记
    如何充分利用C#匿名方法的平台优势(转)
    打开Microsoft SQL Server Management Studio 2005非常慢,特别慢的原因
    PowerShell msbuild
    很久没写了
    第一篇博客,逗大家一笑
    以编程方式调用按钮1(button1)的 Click 事件
    VS2010删除已安装的联机模板
    创建和分享你的Visual Studio color
    异步获取CMD命令行输出内容
  • 原文地址:https://www.cnblogs.com/youcong/p/7944903.html
Copyright © 2011-2022 走看看