linux调用并接收返回函数,shell 函数返回值接收问题

68d908f15c6dc4cb84bb7ed34709b04c.png

注意第一个方式不是单引号!!!

先测试第一个方法:

#!/bin/bash

function check_user(){

if [ "$1"X = "kerry"X ];then

echo "administrator in check_user function"

fi

}

read username

result=`check_user $username`

echo "the result is:"${result}

函数的输出通过标准输出,然后传递给调用函数。结果如下:

[nxuser@PSBJ-0-0-0 tmp]$ ./testfunction.sh

kerry

the result is:administrator in check_user function

如果被调用函数有多个echo输出,返回值是什么呢?是多个集合吗,还是只是一个?

#!/bin/bash

function check_user(){

if [ "$1"X = "kerry"X ];then

echo "administrator in check_user function"

echo "added some other value"

fi

}

read username

result=`check_user $username`

echo "the result is:"${result}

结果如下,是多个echo的集合:

[nxuser@PSBJ-0-0-0 tmp]$ ./testfunction.sh

kerry

the result is:administrator in check_user function added some other value

测试第二个调用方式

#!/bin/bash

function check_user(){

if [ "$1"X = "kerry"X ];then

echo "administrator in check_user function"

echo "added some other value"

fi

}

read username

check_user $username

echo "the result is:"$?

输出结果如下:

[nxuser@PSBJ-0-0-0 tmp]$ ./testfunction2.sh

kerry

administrator in check_user function

added some other value

the result is:0

很明显输出函数执行的返回值,成功为0. 因为没有显示的returen值。现在显示增加return代码

#!/bin/bash

function check_user(){

if [ "$1"X = "kerry"X ];then

echo "administrator in check_user function"

return 8

fi

}

read username

check_user $username

echo "the result is:"$?

结果如下:

kerry

administrator in check_user function

the result is:8

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值