Shell编程-09-Shell中的函数

  函数可以简化程序的代码量,达到更好的代码复用度,因此会让程序变得更加易读、简洁和易修改。其作用就是将需要多次使用的代码整合到一块,使其成为一个整体,然后通过函数名即可完成调用。 基本语法 function functionName () { 语句 return n } 其他简化写法如下 function functionName { 语句 return n } 或 functionName () { 语句 return n } 建议采用非简化的书写方式,方便阅读代码 函数执行 1、执行不带参数的函数,直接输入函数名即可,不需要带括号,如下所示: functionName 执行函数时,函数名前的关键字function和函数名后面的()均不需要带 函数的定义必须要在执行的程序前定义或加载 2、带参数的函数执行语法如下所示: functionName arg1 arg2 > - Shell中的位置参数($1/$2.../$#/$?/$@)均可以做为函数的参数进行传递 > - $0比较特殊,仍然是父脚本的名称 > - 此时父脚本的参数会临时被函数的参数所掩盖或隐藏 > - 函数的参数变量是在函数体内里面进行定义 函数的执行总结如下: 1、Shell各种程序的执行顺序为:系统别名->函数->系统命令->可执行文件等 2、函数执行时,会和调用它的脚本共享变量,也可以为函数设定局部变量及特殊位置参数 3、在Shell函数里面,return和exit功能类似,区别是return是退出函数,exit则是退出脚本 4、return语句会返回一个值给调用函数的程序,exit则会返回一个值给执行当前脚本的Shell 5、如果将函数单独存放为一个文件,在加载时需要使用source或 . 进行加载 6、在函数内部一般使用local定义局部变量,仅在函数体内有效 函数示例 1、示例1:调用函数 [root@localhost Test]# cat testfunction.sh #!/bin/bash # first function function HelloWorld() { echo "Hello world" } # second function Welcome() { echo "Welcome to Shanghai" } # third function function HelloShell { echo "Hello Shell" } # invoke functions HelloWorld # 调用函数 Welcome HelloShell [root@localhost Test]# bash testfunction.sh Hello world Welcome to Shanghai Hello Shell 2、示例2:从文件中调用函数 [root@localhost Test]# cat invokefunction.sh function Sum () { for((i=1;i<=100;i++)) do ((sum=sum+i)) done echo '{1..100} sum is :' $sum } [root@localhost Test]# cat invokefunctionfromfile.sh #!/bin/bash path="/root/Test/invokefunction.sh" if [ -f ${path} ] then source $path # 加载函数 Sum # 调用函数 else echo "file not exist or error" fi [root@localhost Test]# bash invokefunctionfromfile.sh {1..100} sum is : 5050 3、示例3:函数参数传递 [root@localhost Test]# cat functionwithargs.sh #!/bin/bash function Add () { # 定义函数 ((sum=$1+$2)) echo "$1 + $2 sum is" ${sum} } Add $1 $2 # 调用函数并传递参数 [root@localhost Test]# bash functionwithargs.sh 100 150 100 + 150 sum is 250 [root@localhost Test]# bash functionwithargs.sh 509 150 509 + 150 sum is 659 4、示例4:使用return返回函数运行结果 [root@localhost Test]# cat functionwithreturn.sh #!/bin/bash function TestReturn() { if [ -d $1 ] then return "122" else return "222" fi } TestReturn $1 result=$? # 获取函数返回值 if [ ${result} == "122" ] then echo "$1 exist ,return value is:" ${result} else echo "$1 not exist ,return value is:" ${result} fi [root@localhost Test]# bash functionwithreturn.sh /etc/sysconfiggg /etc/sysconfiggg not exist ,return value is: 222 [root@localhost Test]# bash functionwithreturn.sh /etc/sysconfig /etc/sysconfig exist ,return value is: 122 在该示例中,主要通过$?获取返回值,但返回值的范围只能是0~255 5、示例5:使用echo返回函数运行结果 [root@localhost Test]# cat functionwithecho.sh #!/bin/bash function TestReturn() { if [ -d $1 ] then echo "122" else echo "222" fi } result=$(TestReturn $1) # 获取函数返回值 if [ ${result} == "122" ] then echo "$1 exist ,return value is:" ${result} else echo "$1 not exist ,return value is:" ${result} fi [root@localhost Test]# bash functionwithecho.sh /etc/sysconfig /etc/sysconfig exist ,return value is: 122 [root@localhost Test]# bash functionwithecho.sh /etc/sysconfiggg /etc/sysconfiggg not exist ,return value is: 222 在该示例中,主要使用$()获取返回值,在该方法中,没有范围限制,是一种比较安全的返回方式。 [root@localhost Test]# cat functionwithecho.sh #!/bin/bash function TestReturn() { if [ -d $1 ] then echo "$1 exist" else echo "$1 not exist" fi } result=$(TestReturn $1) # 获取返回值,返回的结果是字符串 if [ "${result}" == "$1 exist" ] then echo "$1 exist ,return value is:" ${result} else echo "$1 not exist ,return value is:" ${result} fi [root@localhost Test]# bash functionwithecho.sh /etc/sysconfiggg /etc/sysconfiggg not exist ,return value is: /etc/sysconfiggg not exist [root@localhost Test]# bash functionwithecho.sh /etc/sysconfig /etc/sysconfig exist ,return value is: /etc/sysconfig exist 本文同步在微信订阅号上发布,如各位小伙伴们喜欢我的文章,也可以关注我的微信订阅号:woaitest,或扫描下面的二维码添加关注: MyQRCode.jpg 作者: Surpassme 来源: http://www.jianshu.com/u/28161b7c9995/ http://www.cnblogs.com/surpassme/ 声明:本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出 原文链接 ,否则保留追究法律责任的权利。如有问题,可发送邮件 联系。让我们尊重原创者版权,共同营造良好的IT朋友https://www.cnblogs.com/surpassme/p/10004309.html
50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信