Linux Shell function return value

Source: Internet
Author: User

Shell function return value, generally in three ways: return, argv, Echo

1) Return Statement
The Return Value of the shell function, which can be the same as that of other languages, returned through the return statement.
Example:

#!/bin/bash -function mytest(){    echo "arg1 = $1"    if [ $1 = "1" ] ;then        return 1    else        return 0    fi}echo echo "mytest 1"mytest 1echo $?         # print return resultecho echo "mytest 0"mytest 0echo $?         # print return resultecho echo "mytest 2"mytest 2echo $?         # print return resultechoecho "mytest 1 = "`mytest 1`if  mytest 1 ; then    echo "mytest 1"fiechoecho "mytest 0 = "`mytest 0`if  mytest 0 ; then    echo "mytest 0"fiechoecho "if fasle" # if 0 is errorif false; then    echo "mytest 0"fiechomytest 1res=`echo $?`   # get return resultif [ $res = "1" ]; then    echo "mytest 1"fiechomytest 0res=`echo $?`   # get return resultif [ $res = "0" ]; then    echo "mytest 0"fiecho echo "end"

Result:

Mytest 1
Arg1 = 1
1

Mytest 0
Arg1 = 0
0

Mytest 2
Arg1 = 2
0

Mytest 1 = arg1 = 1
Arg1 = 1

Mytest 0 = arg1 = 0
Arg1 = 0
Mytest 0

If fasle

Arg1 = 1
Mytest 1

Arg1 = 0
Mytest 0

End

First define a function mytest, return 1 or return 0 based on whether the input parameter is 1.
Obtain the return value of a function by calling the function or the value of the last execution.
In addition, the return value of the function can be directly used as the if judgment.
Note: return can only be used to return an integer. The difference between return and C is that return is correct, and other values are incorrect.

2) argv global variables

This is similar to the global variable (or environment variable) in C language ).

Example:

#!/bin/bash -g_var=function mytest2(){    echo "mytest2"    echo "args $1"    g_var=$1    return 0}mytest2 1echo "return $?"echoecho "g_var=$g_var"

Result:

Mytest2
ARGs 1
Return 0

G_var = 1

Function mytest2 returns the result by modifying the value of the global variable.

Note:When the above two methods fail

The two methods described above are generally useful, but there are exceptions.
Example:

#!/bin/bash -function mytest3(){    grep "123" test.txt | awk -F: '{print $2}' | while read line ;do        echo "$line"        if [ $line = "yxb" ]; then            return 0    # return to pipe only        fi    done    echo "mytest3 here "    return 1            # return to main process}g_var=function mytest4(){    grep "123" test.txt | awk -F: '{print $2}' | while read line ;do        echo "$line"        if [ $line = "yxb" ]; then            g_var=0            echo "g_var=0"            return 0    # return to pipe only        fi    done    echo "mytest4 here "    return 1}mytest3echo $?echomytest4echo $?echoecho "g_var=$g_var"

Specifically, the content in the test.txt file is as follows:

456: KKK
123: yxb
123: Test

Result:

Yxb
Mytest3 here
1

Yxb
G_var = 0
Mytest4 here
1

G_var =
We can see that mytest3 does not directly return after return, but executes the statement after the loop body. It also shows that mytest4 is the same. At the same time, in mytest4, modification to global variables does not help, and the value of global variables does not change at all. Why is that?
The author believes that the reason why the return statement does not return directly is that the return statement is executed in the pipeline. The pipeline is actually another sub-process, and the return statement is only returned from the sub-process, the while statement is complete. The statement after the function body will continue to be executed.
Similarly, the global variable is modified in the child process, but the modification of the child process cannot be reflected in the parent process. The global variable is only passed into the child process as an environment variable, the child process does not affect the parent process by modifying its environment variables.
Therefore, when writing Shell functions, you must be clear about where the pipeline (CMD & background process) is returned at the moment.

3) echo Return Value

In shell, the return value of a function has a very safe return method, that is, it is returned by outputting to the standard output. Because the child process inherits the standard output of the parent process, the output of the child process is directly reflected to the parent process. Therefore, the returned value is invalid due to the pipeline.
You only need to obtain the return value of the function.

Example:

#!/bin/bash ############################################### Author : IT-Homer# Date   : 2012-09-06 # Blog   : http://blog.csdn.net/sunboy_2050##############################################function mytest5(){    grep "123" test.txt | awk -F: '{print $2}' | while read line; do        if [ $line = "yxb" ]; then            echo "0"    # value returned first by this function            return 0        fi    done    return 1}echo '$? = '"$?"result=$(mytest5)echo "result = $result"echoif [ -z $result ]       # string is nullthen    echo "no yxb. result is empyt"else    echo "have yxb, result is $result"fi

Result:
$? = 0
Result = 0

Have yxb, result is 0

This method is easy to use, but there are 1.1 things that must be paid attention to, and some things that are not the result cannot be output to the standard, such as debugging information, which can be redirected to a file to solve the problem, note that when using commands such as grep, remember 1>/dev/null 2> & 1 to avoid the output of these commands.

Reference recommendations:

Shell function return value

Shell comparison operators in Linux (recommended)

Linux Shell learning Summary (recommended)

Shell learning notes ---- if condition judgment and judgment Condition

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.