1. Preface
Almost half a year did not write a blog, wasted a long time, work in the chores too much, their own more and more lazy. In order to spur their own growth, or to insist on writing a blog, record their growth.
2. Shell Function Introduction
Grammar:
function ] funname [()]{ action; [return int;] }
Description
(1) can be defined with function fun () or directly fun (), without any parameters.
(2) The parameter returns, can display plus: return returns, if not added, will run the result as the last command, as the return value. Return is followed by the value n (0-255) and can be returned directly via ECHO.
3. Attention
There is a limit in the shell to return by return, with a maximum return of 255, more than 255, then the calculation starts at 0.
Today in the work made this error, resulting in incorrect output value, the test department to mention the bug.
Shell Script Invocation Example:
#!/bin/shEcho"Shell function Operation"func1 () {Local NUM1=100Local num2=100Let sum=$num 1+$num 2 return $sum}func2 () {Local NUM1=100Local num2=155Let sum=$num 1+$num 2 return $sum}func3 () {Local NUM1=100Local num2=156Let sum=$num 1+$num 2 return $sum}func4 () {Local NUM1=100Local num2=156Let sum=$num 1+$num 2 Echo $sum}func1Echo"Called func1:$?"Func2Echo"Called func2:$?"func3Echo"Called func3:$?"sum=' Func4 'Echo"Called Func4:$sum"
The execution results are as follows:
function return value in shell