Linux Shell函数返回值的详细介绍

本文发布时间: 2019-Mar-22
Shell函数返回值,一般有3种方式:return,argv,echo1) return 语句shell函数的返回值,可以和其他语言的返回值一样,通过return语句返回。示例:#!/bin/bash -function mytest(){echo "arg1 = $1"if [ $1 = "1" ] ;then return 1else return 0fi}echoecho "mytest 1"mytest 1echo $? # print return resultechoecho "mytest 0"mytest 0echo $? # print return resultechoecho "mytest 2"mytest 2echo $? # print return resultechoecho "mytest 1 = "`mytest 1`if mytest 1 ; thenecho "mytest 1"fiechoecho "mytest 0 = "`mytest 0`if mytest 0 ; thenecho "mytest 0"fiechoecho "if fasle" # if 0 is errorif false; thenecho "mytest 0"fiechomytest 1res=`echo $?` # get return resultif [ $res = "1" ]; thenecho "mytest 1"fiechomytest 0res=`echo $?` # get return resultif [ $res = "0" ]; thenecho "mytest 0"fiechoecho "end"结果:mytest 1arg1 = 11mytest 0arg1 = 00mytest 2arg1 = 20mytest 1 = arg1 = 1arg1 = 1mytest 0 = arg1 = 0arg1 = 0mytest 0if faslearg1 = 1mytest 1arg1 = 0mytest 0end先定义了一个函数mytest,根据它输入的参数是否为1来return 1或者return 0.获取函数的返回值通过调用函数,或者最后执行的值获得。另外,可以直接用函数的返回值用作if的判断。注意:return只能用来返回整数值,且和c的区别是返回为正确,其他的值为错误。2) argv全局变量这种就类似于C语言中的全局变量(或环境变量)。示例:#!/bin/bash -g_var=function mytest2(){echo "mytest2"echo "args $1"g_var=$1return 0}mytest2 1echo "return $?"echoecho "g_var=$g_var"结果:mytest2args 1return 0g_var=1函数mytest2通过修改全局变量的值,来返回结果。注: 以上两个方法失效的时候以上介绍的这两种方法在一般情况下都是好使的,但也有例外。示例:#!/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 fidoneecho "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 fidoneecho "mytest4 here "return 1}mytest3echo $?echomytest4echo $?echoecho "g_var=$g_var"其中,test.txt 文件中的内容如下:456:kkk123:yxb123:test结果:yxbmytest3 here1yxbg_var=0mytest4 here1g_var=可以看到mytest3在return了以后其实没有直接返回,而是执行了循环体后的语句,同时看到mytest4中也是一样,同时,在mytest4中,对全局变量的修改也无济于事,全局变量的值根本就没有改变。这个是什么原因那?笔者认为,之所以return语句没有直接返回,是因为return语句是在管道中执行的,管道其实是另一个子进程,而return只是从子进程中返回而已,只是while语句结束了。而函数体之后的语句会继续执行。同理,全局变量在子进程中进行了修改,但是子进程的修改没有办法反应到父进程中,全局变量只是作为一个环境变量传入子进程,子进程修改自己的环境变量,不会影响到父进程。因此在写shell函数的时候,用到管道(cmd &后台进程也一样)的时候一定要清楚此刻是从什么地方返回。3) echo 返回值其实在shell中,函数的返回值有一个非常安全的返回方式,即通过输出到标准输出返回。因为子进程会继承父进程的标准输出,因此,子进程的输出也就直接反应到父进程。因此不存在上面提到的由于管道导致返回值失效的情况。在外边只需要获取函数的返回值即可。示例:#!/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 fidonereturn 1}echo '$? = '"$?"result=$(mytest5)echo "result = $result"echoif [ -z $result ] # string is nullthenecho "no yxb. result is empyt"elseecho "have yxb, result is $result"fi结果:$? = 0result = 0have yxb, result is 0这个方式虽然好使,但是有一点一定要注意,不能向标准输出一些不是结果的东西,比如调试信息,这些信息可以重定向到一个文件中解决,特别要注意的是,用到比如grep这样的命令的时候,一定要记得1>/dev/null 2>&1来避免这些命令的输出。


(以上内容不代表本站观点。)
---------------------------------
本网站以及域名有仲裁协议。
本網站以及域名有仲裁協議。

2024-Mar-04 02:08pm
栏目列表