Citation: Usually when you write a shell script, you'll find that there are many places that use the same code or the same functionality. If it's a small code, it doesn't matter. But if you use it more than once and it's the same code, I think you'll feel bored too. To be able to reuse the code, this uses the function.
Warm Tips
The format of the variable assignment is:
变量名=变量值
Precautions:
- The variable name should not precede the dollar "$" symbol. (different from php) equals "="
- There can be no spaces before and after. Unlike the C language, explicit syntax is not required in the shell to declare variables.
- The variable name cannot be directly connected to other characters, and if you want to connect it, you must use parentheses: echo "This is $ (he) llo!"
function Definition Format
function name { commands}
or a
name() {}
This is a little bit like the other languages.
Example 1. no parameter function
A simple shell script that uses function functions
test1.sh
#!/bin/bash# this is a test file for test functionfunction func1 { echo"this is an example of function!"}count=1echo"count‘s value is $count."while$count5 ]do func1 $count1 ]doneecho"end of while."
Run:
sh test1.sh
Output:
Count ' s value is 1.
This is an example of function!
This is an example of function!
This is an example of function!
This is an example of function!
This is an example of function!
End of while.
Example 2. with parameter function
test2.sh
#!/bin/bash# This was a test file for test functionfunction Addem {if[$# -eq 0] || [$# -GT 2] Then Echo-1 elif[$# -eq 1] Then Echo$[ $+ $]Else Echo$[ $+ $]fi}Echo-N"Adding and 5:"Value= ' AddemTen the`Echo $valueEcho-N"Let's try adding just one number:"Value= ' AddemTen`Echo $valueEcho-N"Now trying adding no number:"Value= ' Addem 'Echo $valueEcho-N"Finally,we try adding three numbers:"Value= ' AddemTen the -`Echo $valueEcho "End of file."
Test:
sh test2.sh
Output:
Adding and 5:25
Let ' s try adding just one number:20
Now trying adding no number:-1
Finally,we try adding three numbers:-1
End of file.
If you want to declare a variable inside a function, you can define it by using local, which is represented as a local variable. such as local temp=1
.
Example 3. Array Parameters
test3.sh
#!/bin/bash Span class= "hljs-comment" ># trying to pass a array variable function testit {local newarray # use ' local ' define a local variable newarray= (' echo " the NewArray value is ${newarray[*]} "}myarray= (1 , 2 , 3 , 4 , 5 ) echo "the original array is:${ Myarray[*]} " testit $myarray
Test:
sh test3.sh
Output:
The original array is:1,2,3,4,5
The NewArray value is 1,2,3,4,5
Example 4. function Recursion
Define the factorial function x! = x * (x-1)
function factorial { if$1-eq1 ] then echo1 else $11 ] $temp` echo $[$result$1] fi}
Detailed Example: test4.py
#!/bin/bash# using recursionfunction factorial { if$1-eq1 ] then echo1 else $11 ] $temp` echo$result$1 ] fi}
Test:
sh test4.py
Output:
Enter Value:6
The factorial of 6 is:720
Example 5. Using function commands on the command line
[root@master chapter16]# function multm {$[$1$2 ]> }[root@master chapter16]# multm 2 510
Note: When defining a function on the command line, if you give the function the same name as the built-in command or another command, the function overwrites the original command.
If you want to power on, you can unload the function into the/ETC/.BASHRC file.
Linux Learning-Advanced shell scripting (a) function