"Shell" function

1. Shell function

The essence of the Shell function is a piece of 重复使用的脚本代码code, which has been written in advance, placed in a specified location, and can be called directly when used.
The functions in the Shell are similar to the functions in other programming languages ​​such as C++, Java, Python, and C#, but the syntax details are different.
Shell function definition:

method 1:

function 函数名   {
    
    
​     命令序列
}

Method 2:

函数名()         {
    
    
​      命令序列
}

Explanation of each part:

  • function: is a keyword in Shell, specially used to define functions;
  • Command sequence: It is the code to be executed by the function, that is, a set of statements;
  • The enclosed { }part is called the function body, and calling a function actually executes the code in the function body.

2. Function return value

returnIt means to exit the function and return an exit value, $?which can be displayed with variables in the script;
usage principles:

  • 1. Get 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, and the value will be divided by256取余

Example 1:

#!/bin/bash
function  test01  {
    
    
   read  -p  "请输入一个整数: "    num
   return     $[num *2]
} 
test01
echo $? 

Edit the script, then test it
insert image description here

Example 2:

#!/bin/bash
function  test02  {
    
    
    read  -p   "请输入一个整数: " num
    echo   $[num * 2]
}
res=$(test02)
echo $res

Edit the script, then test it
insert image description here

3. Function parameter passing

How to pass parameters to functions

  • $1 $2 inside the function body represents the positional parameters following the function when calling the function

  • The $# in the function 体内represents the number of parameters following the function when calling the function

  • $@ $* in the function 体内represents all the parameters following the function when calling the function

  • $1 $2 in the body of the function represents the positional parameters following the script when the script is executed

  • In the function 体外, $# represents the number of parameters following the script

  • In the function 体外, $@ represents all the parameters following the script

Regardless of whether it is inside the function body or outside the function body, $0 represents the script itself

sum1   ()    {
    
    
​    sum=$[$1 + $2]echo $sum
}

read -p "Enter the first parameter:" first
read -p "Enter the second parameter:" second
suml $first $second

Example 1:

#!/bin/bash
sum1() {
    
    
    sum=$[$1 + $2]
    echo $sum
}
read -p "输入第一个传入参数: "  first
read -p "输入第二个传入参数: "  second
sum1 $first  $second

insert image description here

Example 2:

#!/bin/bash
// 定义函数
sum2() {
    
          
//函数体内部的$1 $2代表的是调用函数时,函数后面跟的位置参数
  sum=$[$1 - $2]      
  echo $sum
}
//调用函数
//函数体外$1 $2代表的是执行脚本是,脚本后面跟的位置参数
sum2 $2  $1    

insert image description here

Example 3:

#!/bin/bash
sum2() {
    
    
   sum=$[$1 - $2]
   echo $sum
   echo "在函数体内部的\$#代表调用函数时,函数后面跟的参数个数,当前函数后面有$#个参数"
   echo "在函数体内部的\$@代表调用函数时,函数后面跟的所有参数,当前函数后面的参数有:$@" 
   echo "在函数体内部,\$0代表$0"
}
#调用函数
##函数体外的$1  $2代表的是执行脚本时,脚本后面跟的位置参数
echo "在函数体外时,\$#代表脚本后面跟的参数个数,当前脚本后面有$#个参数"
echo "在函数体外时,\$@代表脚本后面跟的所有参数,当前脚本后面参数有:$@"
echo  "在函数体外,\$0代表$0"
sum2 $2  $1
~           

insert image description here

4. The scope of function variables


By default , the function can only be valid in the shell environment in the script (executing the script with source will also affect the current shell environment of the system)
. restricted to use within the function body

Five. Recursion

function calls its own function
Example 1:
Output all directories contained in the environment variable PATH and its subdirectories and all non-executable files through the
insert image description here
script Example 2:
Define the recursive directory of this function

insert image description here
Example 3:
Define a function for converting decimal to binary
insert image description here
Example 4:
Define a function for splitting IP
insert image description here

Guess you like

Origin blog.csdn.net/Wanghwei17/article/details/130580231