Shell脚本之函数

199 阅读2分钟

函数

函数的作用

  1. 使用函数可以避免代码重复;
  2. 使用函数可以将一个大的工程分割为若干小的功能模块,代码的可读性更强。

函数的使用方法

  1. 先定义函数
  2. 再引用函数

定义函数的方法

两种基本格式;

 #格式一:
 function 函数名 {          //三部分之间都要有空格
    命令序列
 }
 
 #格式二:
 函数名 () {               //函数名和小括号之间的空格可有可无
    命令序列
 }
[root@localhost ~]# km () {
> echo "hello"
> }
[root@localhost ~]# km
hello

image.png

删除函数(unset)

格式:

unset 函数名
[root@localhost ~]# unset hello
[root@localhost ~]# hello
bash: hello: 未找到命令...

image.png

查看函数(declare)

格式:

 declare -F      //查看函数列表
 declare -f      //查看函数具体的定义
[root@localhost ~]# declare -F 
declare -f __HOSTNAME
declare -f __SIZE
declare -f __SLAVEURL
declare -f __VOLNAME
declare -f __expand_tilde_by_ref
declare -f __get_cword_at_cursor_by_ref
declare -f __ltrim_colon_completions
declare -f __parse_options
declare -f __reassemble_comp_words_by_ref
declare -f _allowed_groups
declare -f _allowed_users
declare -f _available_fcoe_interfaces
declare -f _available_interfaces
declare -f _cd
--------------------------------

image.png

函数的返回值

获得函数返回值的两种方式:

  1. return表示退出函数并返回一个退出值,脚本中可以用$?变量显示该值。
  2. 在函数体中用 echo 输出返回值。并在函数体外使用变量赋值后,可再进一步对函数的返回值进行加工操作。

return

使用原则:

  1. 函数一结束就去返回值,应为$?变量只返回执行的最后一条命令的退出返回码
  2. 退出码必须是0-255,超出的值将为除以256取余
[root@localhost opt]# vim 1.sh

#!/bin/bash

fun(){
   read -p"请输入一个数字:" num
   return $[$num*2]
}

fun
echo $?

[root@localhost opt]# ./1.sh
请输入一个数字:20
40
[root@localhost opt]# ./1.sh
请输入一个数字:140
24

image.png

echo

因为return的返回值的范围是0-255,超过部分除以256取余,得不到我们想要的结果,此时可以直接在函数体中使用echo。

[root@localhost opt]# vim 2.sh

#!/bin/bash
test1 () {
        read -p "请输入一个数字:" num
        echo $[$num*2]


}

result=`test1`
echo $result


[root@localhost opt]# ./2.sh
请输入一个数字:40
80
[root@localhost opt]# ./2.sh
请输入一个数字:140
280

image.png

函数的传参数

例1:

[root@localhost opt]# vim sum1.sh


 sum1 () {
     sum=$[$1+$2]
     echo $sum
  }
  
  read -p "请输入第一个位置参数:" first
  read -p "请输入第一个位置参数:" second
  sum1 $first $second
  
[root@localhost opt]# ./sum1.sh
请输入第一个位置参数:15
请输入第一个位置参数:21
36

例2:

[root@localhost opt]# ./sum2.sh

sum2 () {
        sum=$[$1 + $2]
        echo $sum
}
sum2 10 20

[root@localhost opt]# ./sum2.sh
3

函数的作用范围

  1. 函数在Shell脚本中仅在当前Shell环境中有效。(即定义和调用函数要在同一个shell环境中)

  2. 脚本中定义的变量,仅在当前shell环境中有效。

    • 如果使用source或 . 执行脚本,那么该变量在命令行也会生效,因为这两种方式没有打开子shell环境,而是在当前shell环境中执行脚本。一般建议不要使用source和. 来执行脚本,可能会影响系统资源配置。
  3. Shell脚本中变量默认全局有效。(即在整个脚本中都生效,并非是环境变量)

  4. 将变量限定在函数内部使用 local 命令。这样变量只在当前函数内有效,不影响脚本中的其他函数和变量。

例1:

 #!/bin/bash
 i=10
 i=5
 echo $i

image.png

例2:

#!/bin/bash

func2 (){
    i=4
    echo $i
}

i=15
func2
echo $i

image.png

例3:

#!/bin/bash

func3 (){
      local i=10
      echo $i
}

i=21
func3
echo $i

image.png

函数的递归

递归就是一个函数在它的函数体内调用它自身。执行递归函数将反复调用其自身,每调用一次就进入新的一层。所以递归要有两个基本要素,结束条件与递推关系。

递归的两个基本要素:

(1)边界条件:确定递归到何时终止,也称为递归出口。

(2)递归模式:大问题是如何分解为小问题的,也称为递归体。递归函数只有具备了这两个要素,才能在有限次计算后得出结果 。

例:递归计算阶乘

fact () {

if [ $1 -eq 1 ]
then
echo  1
else
 local  tmp=$[$1-1]
 local  res=$(fact $tmp)
echo $[$1 * $res]  
fi
}

read -p "请输入计算阶乘的数字:"  num
res=$(fact $num)
echo $res

image.png