Shell function-parameter passing, return value, recursion, function library (detailed explanations and illustrations that can be understood by the cobbler)

Overview

  • Shell function is to write the command sequence together in a format, and it is convenient to use the command sequence repeatedly
  • It can also be understood that a function is actually an alias for a series of commands

How to define Shell functions

[root@localhost ~]# ./16.sh 
3
5
7
9
11
[root@localhost ~]# vim 16.sh

#!/bin/bash
#function q1 {
    
                                        【定义函数方式一】
#w=$[$1 + $2]
#echo $w
#}

q1() {
    
                                                【定义函数方式二】
w=$[$1 + $2]
echo $w
}
【定义函数q1执行“变量w等于参数1+参数2的值”并输出】

for ((q=1;q<=5;q++))
{
    
    

q1 $q $q+1
}
【将第一个参数q和第二个参数q+1的值输入到函数q1里进行相关命令的执行】

Function return value

  • return means to exit the function and return an exit value, which can be displayed by the $? variable in the script
  • Principles of Use
    • 1. Take the return value as soon as the function ends, because the $? variable only returns the exit status code of the last command executed
    • 2. The exit status code must be 0-255, when the status code exceeds 255, divide by 256 and take the remainder

Method 1 of returning function value

[root@localhost ~]# ./17.sh
请输入数值: 500
232                                    【因为输入500,返回值为500*2,超255,所以除256取余】
[root@localhost ~]# vim 17.sh

#!/bin/bash

qz1() {
    
    
  read -p "请输入数值: " w
  return $[$w * 2]

}

qz1
echo $?

Method 2 of returning function value

  • Not subject to the upper limit of return value 255
[root@localhost ~]# ./18.sh 
请输入:500
1000
[root@localhost ~]# vim 18.sh

#!/bin/bash

qz1(){
    
    

  read -p "请输入:" w
  echo $[$w * 2 ]


}
qz2=`qz1`    【用反撇号提取函数qz1输出的值并赋给变量qz2】
echo $qz2    【输出变量qz2的值】

The way of passing parameters in a function

[root@localhost ~]# ./19.sh 
输入第一个参数值:3  
输入第二个参数值:9
12
[root@localhost ~]# vim 19.sh

#!/bin/bash
qz1() {
    
    
w=$[$1 + $2]
echo $w

}

read -p "输入第一个参数值:" one
read -p "输入第二个参数值:" two
qz1 $one $two

Obtain the value of custom calculation (10000-100000)*2 by passing parameters

[root@localhost ~]# ./20.sh 10000 100000
9900110000
[root@localhost ~]# vim 20.sh

#!/bin/bash
qz1() {
    
    
qz=0
for ((w=$1; w<=$2; w++))
do
  qz=$[$qz + $w*2]                  【计算公式,并将值赋给qz】

done
echo $qz  

}

qz2=`qz1 $1 $2`                    
【这里的参数1和参数2是命令行的位置参数,并将获得到的位置参数输入到函数内进行命令操作。并通过反撇号获取“当函数qz1的参数1和参数2分别是
命令行输入的参数结果时的值”】
echo $qz2   
【最终通过echo输出变量qz2获取到的值】

The scope of function variables

  • Functions in Shell scripts are only valid in the current Shell environment
  • Variables in Shell scripts are globally effective by default
  • Use the local command to limit the variable to the function

What are the values ​​obtained when using local and not using local

[root@localhost ~]# ./21.sh 
9
11
13
2
5
8
2
6
[root@localhost ~]# vim 21.sh

#!/bin/bash
qz() {
    
               【函数名为qz】
echo $w          【输出新变量w的值=11】
w=$[$w+2]        【再次定义新w的值为上一个w的值+2】
echo $w          【输出重新定义的变量w值,也就是13】 
w=2              【再次定义变量w=2】
echo $w          【输出刚定义的变量w,也就是2】
w=6              【再次定义变量w的新值为6,但函数内没有echo进行输出操作】
local w          【将变量w限定在函数内部可使用】
w=4              【定义变量w=4】
w=$[$w+1]        【定义新的变量w是上一个w+1的值】
echo $w          【输出新变量w的值,也就是5】
w=8              【再次定义变量w值为8】  
echo $w          【输出刚定义的变量w值为8}

w=9
echo $w          【输出变量w=9的值】
w=$[$w+2]        【重新定义变量w值等于之前w值+2,也就是11】
qz               【运行函数】

echo $w          【输出函数内定义的w=6值】
            

Recursion

  • Recursion is the operation of a function calling its own function

Recursive factorial

  • The factorial of a positive integer is the product of all positive integers less than or equal to the number, and the factorial of 0 is 1. The factorial of the natural number n is written as n!
  • That is, n!=1×2×3×...×(n-1)×n. Factorial can also be defined recursively: 0!=1, n!=(n-1)!×n
[root@localhost ~]# ./23.sh 
输入你想要阶乘的数值:10
3628800
[root@localhost ~]# ./23.sh 
输入你想要阶乘的数值:20
2432902008176640000
[root@localhost ~]# vim 23.sh

#!/bin/bash
qz() {
    
                                      【函数名为qz】
  if [ $1 -ne 1  ];then                 【进行位置参数1(输入的想要阶乘数值num)不等于1的判断】  
    local qz1=$[$1-1]                   【定义变量qz1=位置参数1的值-1,并用local进行限定,使其只在函数内部可使用】
    local qz2=`qz $qz1`                 【定义变量qz2=函数qz里的变量qz1命令运行后的结果,并用反撇号进行提取,用local进行限定。
                                          因为每次都需要获得位置参数11的值,所以不能直接qz2=qz1,需要定义为qz函数内的qz1值】
    echo $[$1*$qz2]                     【输出值为位置参数1*变量qz2的值】

  else
    echo 1
  fi
}

read -p "输入你想要阶乘的数值:" num
qz $num                                【将变量num传参到函数qz内】

Recursive directory

method one

  • When using wildcard *, when there is no file or directory, use * to indicate
[root@localhost ~]# ./24.sh 
输入你想要查询的目录名:/var/log
/var/log/anaconda: 
......
......
/var/log/secure-20210309
/var/log/speech-dispatcher: 
 /var/log/speech-dispatcher/ *  【前面2个空格以突显层级关系,通配符*代表该目录中无文件】
/var/log/tallylog
......
/var/log/yum.log

[root@localhost ~]# vim 24.sh
#!/bin/bash
qz() {
    
    
for w in $1/ *              【用for循环输出参数1,通配符*代表输入的参数下所有的文件和子目录,并赋值给变量w】
do
  if [ -d $w ];then        【用-d判断变量w是否为目录,因为变量w是输入的绝对路径】
     echo "$2$w: "         【如果条件成立则输出变量w的值,这里的$2在第一次循环时是无值,则没有空格】
     qz $w " $2"           【调用qz函数本身,当第二次递归操作时则有1个空格,第三次递归操作则有2个空格,以表示层级关系】  
  else
     echo "$2$w"           【如果条件不成立,则输出第N此递归得到的空格及w的值】
  fi
done
}

read -p "输入你想要查询的目录名:" dir
qz $dir ""                  【将输入的值进行传参到qz函数内,第二个位置参数""用来缩进以区分子目录】

Way two

  • With ls, there is no display when there is no file or directory
[root@localhost ~]# vim 24.sh
[root@localhost ~]# ./24.sh 
输入你想要查询的目录名:/var/log
......
......
tuned: 
 tuned.log
vmware-vgauthsvc.log.0
[root@localhost ~]# vim 24.sh

#!/bin/bash

qz() {
    
    
for w in $(ls $1)
do
  if [ -d $1/$w ];then
     echo "$2$w: "
     qz $1/$w " $2"
  else
     echo "$2$w" 
  fi

done

}

read -p "输入你想要查询的目录名:" dir
qz $dir ""

Function library

  • Encapsulate frequently used repetitive codes into function library files
  • The function library is generally not executed directly, but called by other scripts
[root@localhost ~]# ./25.sh 132 2
加法的结果为:134
减法的结果为:130
乘法的结果为:264
除法的结果为:66
[root@localhost ~]# ./25.sh 132 0
加法的结果为:132
减法的结果为:132
乘法的结果为:0
除法的结果为:除数不能为0[root@localhost ~]# vim mykun.sh    【定义函数库里的相关操作】
#!/bin/bash
  echo $[$1 + $2]
}

jianfa() {
    
    
  echo $[$1 - $2]
}

chengfa() {
    
    
  echo $[$1 * $2]
}

chufa() {
    
    
  if [ $2 -ne 0 ];then
    echo $[$1 / $2]
  else
    echo "除数不能为0!"
  fi
}

[root@localhost ~]# vim 25.sh  【定义函数】

. /root/mykun.sh               【.或者source+脚本路径,因为函数库里定义了/bin/bash,所以这里的调用则不需要再次定义】

a=$1                           【代表命令行里的第一个位置参数】
b=$2                           【代表命令行里的第二个位置参数】
qz1=`jiafa $a $b`              
【变量qz1=通过命令行位置参数获取的变量a和变量b的值传参到函数库的jiafa函数里,并通过反撇号提取jiafa函数获得传参后的运行结果】
qz2=`jianfa $a $b`
qz3=`chengfa $a $b`
qz4=`chufa $a $b`

echo "加法的结果为:$qz1"
echo "减法的结果为:$qz2"
echo "乘法的结果为:$qz3"
echo "除法的结果为:$qz4"

Guess you like

Origin blog.csdn.net/TaKe___Easy/article/details/114643395