Linux Shell function return value _linux Shell

Source: Internet
Author: User

The Shell function returns the value, generally has 3 kinds of ways: Return,argv,echo

1) Return statement
The return value of the Shell function, as in other languages, can be returned through the return statement.
Example:

#!/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"  C40/>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 "my Test 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 argument entered is a return 1 or return 0.
Gets the return value of the function by calling the function, or by the last executed value.
Alternatively, you can use the return value of the function directly as an if judgment.
Note: Return can only be used to return an integer value, and the difference from C is that it is returned as correct and the other values are errors.

2) argv global variable
This is similar to the global variable (or environment variable) in the C language.
Example:

#!/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

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 helpful, but there are exceptions.
Example:

#!/bin/bash-


function mytest3 ()
{
  grep "123" test.txt | awk-f: ' {print $} ' | While read line;d o
    Echo $line "
    if [$line =" YXB "] then return
      0 # return to  pipe only fi
    do

  echo" Mytest3 here "
   return 1      # Return to main process
}

g_var=
function mytest4 ()
{
  grep ' 123 ' Test.txt | Awk-f: ' {print $} ' | While read line;d o
    echo "$line"
    if [$line = "YXB"]; then
      g_var=0
      echo "g_var=0" return
      0  # return to pipe only
    fi

  do echo ' Mytest4 here ' return
  1
}

mytest3
echo $?

echo
Mytest4
echo $?

echo
echo "g_var= $g _var"

Among them, 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=
You can see MYTEST3 in return after actually not directly returned, but the execution of the statement after the loop body, and see the same in Mytest4, at the same time, in Mytest4, the changes to the global variables do not help, the value of global variables have not changed. What's the reason for that?
In my opinion, the return statement is not returned directly, because a return statement is executed in a pipe, and the pipe is actually another child process, but returns are only returned from the child process, but the while statement ends. The statement that follows the function body continues to execute.
Similarly, a global variable is modified in a subprocess, but the modification of the child process is not reflected in the parent process, and the global variable is passed into the subprocess as an environment variable, and the child process modifies its own environment variables without affecting the parent process.
So when writing a shell function, use the pipe (cmd & background process is the same) when you must know where to return from where the moment.

3) Echo return value
In fact, in the shell, the return value of the function has a very safe way of returning, that is, through output to standard output. Because the child process inherits the standard output from the parent process, the output of the child process is directly reflected to the parent process. Therefore, there is no condition mentioned above that caused the return value to be invalidated by the pipe.
You just need to get the return value of the function outside.
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 $} ' | while read line; does
    if [$line = ' yxb ']; Then
      echo "0"  # value returned the-I-function return
      0 fi do return

  1
}< C19/>echo ' $? = '"$?"
result=$ (MYTEST5)

echo "result = $result"

echo
if [-Z $result]    # string is null
then
  echo "No YXB. Result is Empyt '
else
  echo ' have yxb, result is $result '
fi

Results:
$? = 0
result = 0

Have YXB, result is 0
This is a good way to do it, but it's important to note that you can't output something that is not a result to the standard, such as debugging information, which can be redirected to a file to resolve, especially when using a command such as grep, be sure to remember 1>/dev/null 2 >&1 to avoid the output of these commands.

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.