The function return value in Shell exceeds the limit.

Source: Internet
Author: User

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:

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.