函数可以简化程序的代码量, 达到更好的代码复用度, 因此会让程序变得更加易读, 简洁和易修改. 其作用就是将需要多次使用的代码整合到一块, 使其成为一个整体, 然后通过函数名即可完成调用.
基本语法
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
来源: https://www.cnblogs.com/surpassme/p/10004309.html