The function return value in Shell exceeds the limit.
This article mainly introduces the function return value in Shell that exceeds the limit. In shell, return is restricted. If the return value exceeds 255, the return value starts from 0, for more information, see
1. Preface
I haven't written a blog for nearly half a year. I 've been ruined for a long time, and I 've got too many chores at work, so I'm getting lazy. To spur your own growth, you still need to write a blog and record your own growth.
2. Introduction to shell functions
Syntax:
The Code is as follows:
[Function] funname [()]
{
Action;
[Return int;]
}
Note:
(1) It can be defined with function fun () or fun () without any parameters.
(2) If the parameter is returned, the result of the last command is displayed as the return value. Return is followed by the value n (0-255), and can be directly returned through echo.
3. Note
In shell, return is restricted. If the return value is greater than 255, the return value starts from 0.
This error was made at work today, resulting in incorrect output values and a bug was reported by the test department.
Shell script call example:
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 = 'funcsv'
Echo "called func4: $ sum"
The execution result is as follows: