Function, return value, parameter passing, function call function and recursion in shell script, function library

One, shell function

Write the command sequence together in a format, and the command sequence
can be reused

1. Shell function definition format:


1.
function 函数名{
    
     

   命令序列
   
 }

2.
  函数名(){
    
     
  
    命令序列
    
  }

2. Function return value

return

return means to exit the function and return an exit value. The value (0-255) can be displayed by the $? variable in the script.
Principles:
1. The return value is taken when the function is over, because $? is just a judgment on the last command
2.0-255 , When it is exceeded, divide by 256 and take the remainder
echo can also return directly to the
experiment code



[root@rain opt]#vim aaa.sh

#!/bin/bash
#测试

function hs {
    
    
   read -p "请输入任一个值" v
   return $[$v*2]

}

hs
echo "$?"

Insert picture description here
Insert picture description here

echo to return

When using echo below to return
Insert picture description here
Insert picture description here

3. Function parameter transfer

  • The scope of function variables (local variables and global variables)
  • Functions in shell scripts are only valid in the current shell environment
  • Variables in shell scripts are globally effective by default
  • Limit the variable to the internal local of a function, that is, a local variable

Transfer parameters



[root@rain opt]#vim demo22.sh

#!/bin/bash
#
 sum1 () {
    
    

sum=$[ $1 + $2 ]
echo $sum


}

sum1 $1 $2

Insert picture description here

[root@rain opt]#sh demo22.sh 20 30
50

Insert picture description here

Local and global variables

Use the local command to limit the variable to the function
, that is, the local variable is only valid in the current function, and invalid in other functions.

[root@rain opt]#vim demo23.sh

#!/bin/bash

abc () {
    
    
echo "函数内的未经过local的$i"
local i
i=6
echo "函数内的是$i"


}

i=9
abc
echo "函数外面的$i"

Insert picture description here
Results of the:
Insert picture description here

Local attack:

Insert picture description here

4. Recursion (call yourself)

Insert picture description here

Insert picture description here
Insert picture description here
The first time: the total value is the value of jc 2 multiplied by 3.
At this time, the input value has changed from the original 3 to 2. Repeat the above steps again

Second time: the total value of jc 1 is multiplied by 2 and 3, which
is 1 2 3

5. Function library

It is to separate the main execution code and the defined function part of the general script from each other, and put the defined function part together in the same script. During specific execution, you only need to directly call the script file of the function set. A bit similar to the visual sense of object-oriented programming in java.
Here I made a library of 4 functions including addition, subtraction, multiplication, and division.

#!/bin/bash
#函数库

jia(){
    
    
  result=$[ $1 + $2 ]
   echo "$result" 
}

jian(){
    
    
  result=$[ $1 - $2 ]
  echo "$result"
}

cheng(){
    
    
  result=$[ $1 * $2 ]
  echo "$result"
}

chu(){
    
    
  if [ $2 -ne 0 ]
  then
  result=$[ $1 / $2 ]
  echo "$result"
  else
  echo"除法所输入不能为0"
  fi
}

Insert picture description here
The script is executed here, and the calculation is completed by calling the function library.


#!/bin/bash
. /opt/ku.sh

#加减乘除

read -p "请输入第一个数字" f
read -p "请输入第二个数字" s

result1=`jia $f $s`
result2=`jian $f $s`
result3=`cheng $f $s`
result4=`chu $f $s`

echo "第一个数字与第二个数字的和为:$result1"
echo "第一个数字与第二个数字的差为:$result2" 
echo "第一个数字与第二个数字的积为:$result3" 
echo "第一个数字与第二个数字的商为:$result4" 

Insert picture description here
Results of the:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44324367/article/details/111506721