Linux Shell function return value

Source: Internet
Author: User






Ext.: http://blog.csdn.net/ithomer/article/details/7954577






Shell function return value, generally there are 3 ways: Return,argv,echo












1) Return statement

The return value of the Shell function, which can be returned by a return statement, as the return value of other languages.

Example:










[JavaScript]View PlainCopyprint?

#!/bin/bash-

function mytest ()

{

echo "arg1 = $"

If [$ = "1"]; Then

return 1

Else

return 0

Fi

}

Echo

Echo "MyTest 1"

MyTest 1

echo $? # print return result

Echo

echo "MyTest 0"

MyTest 0

echo $? # print return result

Echo

Echo "MyTest 2"

MyTest 2

echo $? # print return result

Echo

Echo "MyTest 1 =" ' MyTest 1 '

If MyTest 1; Then

Echo "MyTest 1"

Fi

Echo

echo "MyTest 0 =" ' mytest 0 '

If mytest 0; Then

echo "MyTest 0"

Fi

Echo

echo "If Fasle" # if 0 is error

If false; Then

echo "MyTest 0"

Fi

Echo

MyTest 1

Res= ' echo $? ' # get return result

if [$res = "1"]; Then

Echo "MyTest 1"

Fi

Echo

MyTest 0

Res= ' echo $? ' # get return result

if [$res = "0"]; Then

echo "MyTest 0"

Fi

Echo

echo "End"









Results:






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






A function mytest is defined first, depending on whether the parameter it enters is a return 1 or a return 0.

Gets the return value of the function, either by calling the function, or by the last executed value.

Alternatively, you can use the return value of the function directly as the IF judgment.

Note: Return can only be used to return an integer value, and the difference to C is returned as correct, and the other value is error.












2) argv Global variables






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






Example:




[JavaScript]View PlainCopyprint?

#!/bin/bash-

G_var=

function Mytest2 ()

{

echo "Mytest2"

Echo "args $"

G_var=$1

return 0

}

Mytest2 1

Echo "return $?"

Echo

echo "g_var= $g _var"



Results:






Mytest2

Args 1

return 0


G_var=1







The function Mytest2 returns the result by modifying the value of the global variable.












Note: when the above two methods fail






Both of the methods described above are generally helpful, but there are exceptions.

Example:




[JavaScript]View PlainCopy print?

#!/bin/bash-

function Mytest3 ()

{

grep "123" Test.txt | awk-f: ' {print $} ' | 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 $} ' | 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

}

Mytest3

echo $?

Echo

Mytest4

echo $?

Echo

echo "g_var= $g _var"









Where the contents of the Test.txt file are as follows:






456:kkk

123:yxb

123:test






Results:






Yxb

Mytest3 here

1


Yxb

G_var=0

Mytest4 here

1


G_var=

Can see MYTEST3 in return after actually did not directly return, but executed the loop body after the statement, at the same time see Mytest4 is the same, at the same time, in Mytest4, the changes to the global variable is useless, the value of the global variable is not changed at all. What's the reason for that?

I believe that the return statement does not return directly, because the return statement is executed in the pipeline, the pipeline is actually another child process, and return is only returned from the child process, but the while statement is finished. The statement after the function body continues execution.

Similarly, the global variable is modified in the subprocess, but the modification of the child process is not reflected in the parent process, the global variable is simply passed into the child process as an environment variable, and the child process modifies its own environment variable without affecting the parent process.

So when you write Shell functions, use the same pipeline (cmd & daemon) When you need to know where to return from.












3) Echo return value






In fact, in the shell, the return value of the function has a very safe return, which is returned by output to standard output. Because the child process inherits the standard output from the parent process, the output of the child process is also reflected directly to the parent process. Therefore, there is no case of the above mentioned because the pipeline causes the return value to fail.

On the outside you just need to get the return value of the function.






Example:










[JavaScript]View PlainCopy print?

#!/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 $} ' | 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"

Echo

If [-Z $result] # string is null

Then

echo "no YXB. Result is Empyt "

Else

Echo "has yxb, result is $result"

Fi



Results:

$? = 0

result = 0


Has YXB, result is 0












Although this is a better way, it is important to note that it is not possible to output something that is not a result, such as debugging information, which can be redirected to a file, especially when using commands such as grep, remember 1>/dev/null 2 >&1 to avoid the output of these commands.


















Reference recommendation:






Shell function return value






Linux Shell comparison operators (recommended)






Linux Shell Learning Brief summary (recommended)






Shell Learning notes----If condition judgment, judging condition






Linux Shell function return value




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.