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
posted @   Surpassme  阅读(464)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示