Linux learning-advanced shell script programming (I) Functions and shell script Programming
Citation:When writing shell scripts, you will find that the same code or functions are used in many places. It doesn't matter if it's a small piece of code. But if you use the same code multiple times, I think it will be annoying. To make the code reusable, the function is used.
Tip
Variable assignment format:
Variable name = variable value
Note:
- The variable name should not be preceded by the dollar "$" symbol. (Different from PHP) equal sign "="
- There cannot be spaces before and after. Unlike C, Shell does not require explicit syntax to declare variables.
- Variable names cannot be directly connected to other characters. to connect, you must use parentheses: echo "this is $ (he) llo !"
Function Definition Format
function name { commands}
Or
name() {}
This is a bit similar to other languages.
Instance 1. No Parameter Function
A simple shell script using 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 [ $count -le 5 ]do func1 count = $[ $count + 1 ]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.
Instance 2. Function with Parameters
Test2.sh
#!/bin/bash# this is a test file for test functionfunction addem { if [ $# -eq 0 ] || [ $# -gt 2 ] then echo -1 elif [ $# -eq 1 ] then echo $[ $1 + $1] else echo $[ $1 + $2 ] fi}echo -n "Adding 10 and 5:"value=`addem 10 15`echo $valueecho -n "Let's try adding just one number:"value=`addem 10`echo $valueecho -n "Now trying adding no number:"value=`addem`echo $valueecho -n "Finally,we try adding three numbers:"value=`addem 10 15 20`echo $valueecho "end of file."
Test:
sh test2.sh
Output:
Adding 10 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 in the function, you can use local to define it as a local variable. For examplelocal temp=1
.
Instance 3. array parameters
Test3.sh
#!/bin/bash# trying to pass a array variablefunction testit { local newarray # use ‘local’ define a local variable newarray=(`echo "$@"`) 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
Instance 4. Function Recursion
Define the factorial function x! = X * (x-1)
function factorial { if [ $1 -eq 1 ] then echo 1 else local temp=$[ $1 - 1 ] local result=`factorial $temp` echo $[$result * $1] fi}
Detailed example: test4.py
#!/bin/bash# using recursionfunction factorial { if [ $1 -eq 1 ] then echo 1 else local temp=$[ $1 - 1 ] local result=`factorial $temp` echo $[ $result * $1 ] fi}
Test:
sh test4.py
Output:
Enter value: 6
The factorial of 6 is: 720
Instance 5. Use Function commands on the command line
[root@master chapter16]# function multm {> echo $[ $1 * $2 ]> }[root@master chapter16]# multm 2 510
Note: when defining a function on a command line, if you give the function the same name as the built-in command or another command, the function will overwrite the original command.
If you want to enable the function to take effect, you can unmount the function from the/etc/. bashrc file.