How to solve the problem of function return value exceeding in Shell
This article mainly introduces "how to solve the problem of function return value exceeding in Shell". In daily operation, I believe many people have doubts about how to solve the problem of function return value exceeding in Shell. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for everyone to solve the problem of "how to solve the problem of function return value exceeding in Shell". Next, please follow the editor to study!
Introduction to shell function
Syntax:
The code is as follows:
[function] funname [()]
{
Action
[return int;]
}
Description:
(1) it can be defined with function fun () or directly fun () without any parameters.
(2) if the parameter is returned, you can display the plus: return return. If you do not add it, the result will be run with the last command as the return value. Return is followed by the value n (0-255) and can also be returned directly through echo.
Be careful
In shell, the return through return is limited, with a maximum of 255.If it exceeds 255, the calculation starts at 0.
Today, I made this mistake at work, resulting in the incorrect value of the output, and the bug was raised by the test department.
Example of shell script call:
The code is as follows:
#! / bin/sh
Echo "shell function operation"
Func1 ()
{
Local num1=100
Local num2=100
Let sum=$num1+$num2
Return $sum
}
Func2 ()
{
Local num1=100
Local num2=155
Let sum=$num1+$num2
Return $sum
}
Func3 ()
{
Local num1=100
Local num2=156
Let sum=$num1+$num2
Return $sum
}
Func4 ()
{
Local num1=100
Local num2=156
Let sum=$num1+$num2
Echo $sum
}
Func1
Echo "called func1:$?"
Func2
Echo "called func2:$?"
Func3
Echo "called func3:$?"
Sum= `func4`
Echo "called func4:$sum"
The execution result is as follows:
At this point, the study on "how to solve the problem of function return value exceeding in Shell" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!