1. Functions
What is a function?
- A function is a block of function code that can be used many times, a closed (space), and can be called freely in the code. Encapsulation of functions can reduce duplicate code development and improve code utilization. Functions can be passed along, using pre-defined content within the function to process different data parameters that are passed in.
- Functions can also be objects or values, and they can exist in variables, arrays, and objects.
- A function can be passed as a parameter to a function and returned by the function, while the function has properties.
- Functions always have return values (except for constructors, which return constructor function calls by default and display returns when the constructor call is executed)
Basic Shell Function Format
First:
function Function name { Command Sequence }
Second:
Function name{ Command Sequence }
Function return value
Functions can be passed as parameters and returned as return values. Can $be used in scripts? The variable displays the value.
Principles of Use
- The return value is taken as soon as the function ends because $? Variable returns only the exit status code of the last command executed
- Exit status code must be 0~255, exceeding time value will be divided by 256
Give an example:
(1) Defining functions with functions
#!/bin/bash function lizi{ read -p "Please enter a number:" a a=$[$a*2] return $a } lizi echo $? echo "The results are: $a"
(2) Define with lizi() {command sequence}
#!/bin/bash lizi (){ read -p "Please enter a number:" a a=$[$a*2] return $a } lizi echo $? echo "The results are: $a"
Parameter transfer of functions
You can pass parameters to a function when it is called. Inside the function body, the value of the parameter is obtained in the form of $n, for example, $1 for the first parameter, $2 for the second parameter,... Even if the position parameter is used for parameter transfer.
Give an example:
Method 1: Find the product of two numbers
#!/bin/bash function chenji { sum=$[$1 * $2] echo $sum } read -p "Please enter the first parameter:" first read -p "Enter the second parameter:" second chenji $first $second
Method 2: Find the product of two numbers
#!/bin/bash chengji () { sum=$[$1 * $2] echo $sum } chengji $1 $2
Scope of function variables
- Functions are valid in Shell scripts only in the current Shell environment
- Variables in Shell scripts are globally valid by default
- Limit variables to functions using local commands
Give an example:
#!/bin/bash aaaa () { local i #Setting local variables i=8 echo $i } i=9 #Global Environment aaaa echo $i #!/bin/bash aaaaa () { i=8 #local is not added, that is, it is a global variable echo $i } aaaaa echo $i
2. Function Recursion
What is function recursion?
Recursive function call (a special nested call): In the process of calling a function, the function itself is called directly or indirectly.
Recursion must have two distinct phases:
- Recursive: Call recursively one level at a time, emphasizing that the scale of each recursive problem that enters the next level must be reduced
- Backtracking: Recursion must have a clear end condition that ends when it meets the condition and begins one layer at a time.
The essence of recursion is to approach a final result by repeating it over and over again
Typical Function Recursion----Factorial
What is a factorial?
The product of multiplying consecutive natural numbers from 1 to n, called a factorial, with the symbol n! Express. Such as 5!= 1 × 2 × 3 × 4 × 5. Provision 0!= 1
Give an example:
First example: factorial recursion
#!/bin/bash fact (){ if [ $1 -eq 1 ] then echo 1 else local temp=$[$1 - 1] local result=$(fact $temp) echo $[$1 * $result] fi } read -p "Please enter the number you want to calculate:" num result=$(fact $num) echo $result
Example 2: Catalog recursion
#!/bin/bash myfunc (){ for i in $1/* do if [ -d $i ] then echo "$2$i" myfunc "$i" " $2" else if [ -x $i ] then echo "-----$2$i" fi fi done } ###main### OLDIFS=$IFS IFS=$IFS: for a in $(echo $PATH) do myfunc $a "" done IFS=$OLDIFS
3. Function Library
What is a function library?
Personally, the shell function library is essentially a script that contains multiple functions (functions are universally applicable).
Give an example:
New Function Library
1,ku.sh: jiafa() { echo $[$1 + $2] } chengfa() { echo $[$1 * $2] } jianfa() { echo $[$1 - $2] } chufa() { if [ $2 -ne 0 ];then echo $[$1 / $2] else echo "$2 Cannot be zero" fi } jiecheng() { if [ $1 -eq 1 ] then echo 1 else local temp=$(jianfa $1 1) local result=$(jiecheng $temp) echo $(chengfa $1 $result) fi } 2,diaoyong.sh: #!/bin/bash . /root/ku.sh read -p "Enter the first parameter value:" first read -p "Enter the second parameter value:" second result1=`jiafa $first $second` result2=`chengfa $first $second` result3=`jianfa $first $second` result4=`chufa $first $second` result5=`jiecheng $first` echo $result1 echo $result2 echo $result3 echo $result4 echo $result5