1. Preface
Almost six months did not write a blog, deserted for a long time, the work of the chores too much, their own more and more lazy. In order to spur their own growth, or adhere to write a blog, record their growth.
2. Introduction to Shell function
Grammar:
Copy Code code as follows:
[Function] funname [()]
{
Action
[Return int;]
}
Description
(1) can be defined with function fun () or directly fun (), without any arguments.
(2) parameter return, you can show Plus: return returns, if not, will run the result with the last command as the return value. Return is followed by a value of N (0-255) and can be returned directly via ECHO.
3. Note
The shell returns by return is restricted, the maximum returns 255, more than 255, then the calculation starts at 0.
Today in the work made this error, resulting in incorrect output, the test department to mention bugs.
Shell Script Invocation Example:
Copy Code code as follows:
#!/bin/sh
echo "Shell function operation"
Func1 ()
{
Local num1=100
Local num2=100
Let sum= $num 1+ $num 2
Return $sum
}
Func2 ()
{
Local num1=100
Local num2=155
Let sum= $num 1+ $num 2
Return $sum
}
FUNC3 ()
{
Local num1=100
Local num2=156
Let sum= $num 1+ $num 2
Return $sum
}
Func4 ()
{
Local num1=100
Local num2=156
Let sum= $num 1+ $num 2
Echo $sum
}
Func1
echo "called func1:$?"
Func2
echo "called func2:$?"
Func3
echo "called func3:$?"
sum= ' Func4 '
echo "called Func4: $sum"
The results of the execution are as follows: