function return value exceeds problem in shell _linux shell

Source: Internet
Author: User

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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.