函数可以简化程序的代码量,达到更好的代码复用度,因此会让程序变得更加易读、简洁和易修改。其作用就是将需要多次使用的代码整合到一块,使其成为一个整体,然后通过函数名即可完成调用。

基本语法

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,或扫描下面的二维码添加关注:

最新文章

  1. 理解RESTful架构
  2. [转][Android]FragmentPagerAdapter与FragmentStatePagerAdapter使用详解与区别
  3. 编译安装php的配置参数详细解析
  4. Sublime Text 常用快捷键和优秀插件
  5. IOS 登陆界面的简单编写(通过NSNotificationCenter)
  6. Entity Framework 系统约定配置
  7. oracle数据库备份与还原
  8. [设计模式]NetworkManagementService中的观察者模式
  9. python 之路,Day11(上) - python mysql and ORM
  10. 窗口显示于parent控件上(用到了ManualDock函数)
  11. Visual Studio自动添加头部注释 -C#开发2010-2013验证
  12. ios-deploy was not found
  13. 【Thymeleaf】常用属性
  14. querySelector() 方法
  15. ROS rosrun 调用 sudo 命令
  16. substring 比较(c#和Java)
  17. Codeforces Round #515 (Div. 3)
  18. Valid Sudoku leetcode java
  19. python中redis查看剩余过期时间以及用正则通配符批量删除key的方法
  20. java结合js获取验证码

热门文章

  1. 如何调用别人提供的API?
  2. JQuery.validate 错误信息对话框
  3. HDU_2136
  4. grep如何进行正则表达式查找
  5. IOS语法
  6. hdfs结构
  7. zookeeper相关
  8. mybatis高级映射-一对多
  9. 上传文件 input file
  10. Windows server 2008 被ntlmssp安装攻击 解决