Linux Shell函数返回值

转:http://blog.csdn.net/ithomer/article/details/7954577javascript

Shell函数返回值,通常有3种方式:return,argv,echojava

 

1) return 语句
shell函数的返回值,能够和其余语言的返回值同样,经过return语句返回。
示例:shell

 

[javascript]  view plain  copy
 
 print?
  1. #!/bin/bash -  
  2. function mytest()  
  3. {  
  4.     echo "arg1 = $1"  
  5.     if [ $1 = "1" ] ;then  
  6.         return 1  
  7.     else  
  8.         return 0  
  9.     fi  
  10. }  
  11.   
  12. echo   
  13. echo "mytest 1"  
  14. mytest 1  
  15. echo $?         # print return result  
  16.   
  17. echo   
  18. echo "mytest 0"  
  19. mytest 0  
  20. echo $?         # print return result  
  21.   
  22. echo   
  23. echo "mytest 2"  
  24. mytest 2  
  25. echo $?         # print return result  
  26.   
  27.   
  28. echo  
  29. echo "mytest 1 = "`mytest 1`  
  30. if  mytest 1 ; then  
  31.     echo "mytest 1"  
  32. fi  
  33.   
  34. echo  
  35. echo "mytest 0 = "`mytest 0`  
  36. if  mytest 0 ; then  
  37.     echo "mytest 0"  
  38. fi  
  39.   
  40. echo  
  41. echo "if fasle" # if 0 is error  
  42. if false; then  
  43.     echo "mytest 0"  
  44. fi  
  45.   
  46.   
  47. echo  
  48. mytest 1  
  49. res=`echo $?`   # get return result  
  50. if [ $res = "1" ]; then  
  51.     echo "mytest 1"  
  52. fi  
  53.   
  54. echo  
  55. mytest 0  
  56. res=`echo $?`   # get return result  
  57. if [ $res = "0" ]; then  
  58.     echo "mytest 0"  
  59. fi  
  60.   
  61.   
  62.   
  63. echo   
  64. echo "end"  

 

结果:安全

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

endbash

先定义了一个函数mytest,根据它输入的参数是否为1来return 1或者return 0.
获取函数的返回值经过调用函数,或者最后执行的值得到。
另外,能够直接用函数的返回值用做if的判断。
注意:return只能用来返回整数值,且和c的区别是返回为正确,其余的值为错误。函数

 

2) argv全局变量学习

这种就相似于C语言中的全局变量(或环境变量)。this

示例:spa

[javascript]  view plain  copy
 
 print?
  1. #!/bin/bash -  
  2.   
  3. g_var=  
  4. function mytest2()  
  5. {  
  6.     echo "mytest2"  
  7.     echo "args $1"  
  8.     g_var=$1  
  9.   
  10.     return 0  
  11. }  
  12.   
  13. mytest2 1  
  14. echo "return $?"  
  15.   
  16. echo  
  17. echo "g_var=$g_var"  

结果:.net

mytest2
args 1
return 0

g_var=1


函数mytest2经过修改全局变量的值,来返回结果。

 

注: 以上两个方法失效的时候

以上介绍的这两种方法在通常状况下都是好使的,但也有例外。
示例:

[javascript]  view plain  copy
 
 print?
  1. #!/bin/bash -  
  2.   
  3.   
  4. function mytest3()  
  5. {  
  6.     grep "123" test.txt | awk -F: '{print $2}' | while read line ;do  
  7.         echo "$line"  
  8.         if [ $line = "yxb" ]; then  
  9.             return 0    # return to pipe only  
  10.         fi  
  11.     done  
  12.   
  13.     echo "mytest3 here "  
  14.     return 1            # return to main process  
  15. }  
  16.   
  17. g_var=  
  18. function mytest4()  
  19. {  
  20.     grep "123" test.txt | awk -F: '{print $2}' | while read line ;do  
  21.         echo "$line"  
  22.         if [ $line = "yxb" ]; then  
  23.             g_var=0  
  24.             echo "g_var=0"  
  25.             return 0    # return to pipe only  
  26.         fi  
  27.     done  
  28.   
  29.     echo "mytest4 here "  
  30.     return 1  
  31. }  
  32.   
  33. mytest3  
  34. echo $?  
  35.   
  36. echo  
  37. mytest4  
  38. echo $?  
  39.   
  40. echo  
  41. echo "g_var=$g_var"  

 

其中,test.txt 文件中的内容以下:

456:kkk
123:yxb
123:test

结果:

yxb
mytest3 here 
1

yxb
g_var=0
mytest4 here 
1

g_var=
能够看到mytest3在return了之后其实没有直接返回,而是执行了循环体后的语句,同时看到mytest4中也是同样,同时,在mytest4中,对全局变量的修改也无济于事,全局变量的值根本就没有改变。这个是什么缘由那?
笔者认为,之因此return语句没有直接返回,是由于return语句是在管道中执行的,管道实际上是另外一个子进程,而return只是从子进程中返回而已,只是while语句结束了。而函数体以后的语句会继续执行。
同理,全局变量在子进程中进行了修改,可是子进程的修改没有办法反应到父进程中,全局变量只是做为一个环境变量传入子进程,子进程修改本身的环境变量,不会影响到父进程。
所以在写shell函数的时候,用到管道(cmd &后台进程也同样)的时候必定要清楚此刻是从什么地方返回。

 

3) echo 返回值

其实在shell中,函数的返回值有一个很是安全的返回方式,即经过输出到标准输出返回。由于子进程会继承父进程的标准输出,所以,子进程的输出也就直接反应到父进程。所以不存在上面提到的因为管道致使返回值失效的状况。
在外边只须要获取函数的返回值便可。

示例:

 

[javascript]  view plain  copy
 
 print?
  1. #!/bin/bash   
  2.  
  3. ##############################################  
  4. # Author : IT-Homer  
  5. # Date   : 2012-09-06   
  6. # Blog   : http://blog.csdn.net/sunboy_2050  
  7. ##############################################  
  8.   
  9. function mytest5()  
  10. {  
  11.     grep "123" test.txt | awk -F: '{print $2}' | while read line; do  
  12.         if [ $line = "yxb" ]; then  
  13.             echo "0"    # value returned first by this function  
  14.             return 0  
  15.         fi  
  16.     done  
  17.   
  18.     return 1  
  19. }  
  20.   
  21. echo '$? = '"$?"  
  22. result=$(mytest5)  
  23.   
  24. echo "result = $result"  
  25.   
  26. echo  
  27. if [ -z $result ]       # string is null  
  28. then  
  29.     echo "no yxb. result is empyt"  
  30. else  
  31.     echo "have yxb, result is $result"  
  32. fi  

结果:
$? = 0
result = 0

have yxb, result is 0

 

这个方式虽然好使,可是有一点必定要注意,不能向标准输出一些不是结果的东西,好比调试信息,这些信息能够重定向到一个文件中解决,特别要注意的是,用到好比grep这样的命令的时候,必定要记得1>/dev/null 2>&1来避免这些命令的输出。

 

 

参考推荐:

Shell函数返回值

Linux 之 shell 比较运算符(推荐)

Linux Shell学习简单小结(推荐)

SHELL学习笔记----IF条件判断,判断条件

相关文章
相关标签/搜索