[问题]请教如何得到函数返回值
-
- 帖子: 59
- 注册时间: 2006-11-09 21:06
- 来自: 深圳
[问题]请教如何得到函数返回值
我知道一种方法:在函数中echo输出,然后用data="$(func)"命令来得到函数输出值。
可是函数的return命令返回的数字值怎么得到阿,用data="$(func)"试验得不到返回值,比方说:
#/bin/sh
func() {
if [ -f aa.txt ]
then
return 0;
else
return -1;
fi
}
echo "主函数开始:"
result="$(func)"
echo "result=$result"
输出result是一个空串,请大侠指导
可是函数的return命令返回的数字值怎么得到阿,用data="$(func)"试验得不到返回值,比方说:
#/bin/sh
func() {
if [ -f aa.txt ]
then
return 0;
else
return -1;
fi
}
echo "主函数开始:"
result="$(func)"
echo "result=$result"
输出result是一个空串,请大侠指导
-
- 帖子: 59
- 注册时间: 2006-11-09 21:06
- 来自: 深圳
可以得到函数内echo的输出值的,比如把例子改成:
就能得到结果:
[root@localhost shell]# ./func.sh (返回数字值得不到结果)
主函数开始:
result=
[root@localhost shell]# ./func.sh (采用echo得到输出)
主函数开始:
result=-1
[/quote]
代码: 全选
#/bin/sh
func() {
if [ -f aa.txt ]
then
echo "0"
else
echo "-1"
fi
}
echo "主函数开始:"
result="$(func)"
echo "result=$result"
[root@localhost shell]# ./func.sh (返回数字值得不到结果)
主函数开始:
result=
[root@localhost shell]# ./func.sh (采用echo得到输出)
主函数开始:
result=-1
[/quote]
- lonelycorn
- 帖子: 438
- 注册时间: 2007-05-20 18:44
- 来自: 唐山,河北,中国
- 联系:
-
- 帖子: 1074
- 注册时间: 2006-01-18 15:01
-
- 帖子: 59
- 注册时间: 2006-11-09 21:06
- 来自: 深圳
我的shell跟你的一样阿,版本如下:aBiNg 写了:shell中有无return命令,楼主先which return看看。
我的bash:代码: 全选
[18:56 ~] $ bash --version GNU bash, version 3.1.17(2)-release (i486-slackware-linux-gnu) Copyright (C) 2005 Free Software Foundation, Inc.
# bash --version
GNU bash, version 3.1.17(1)-release (i686-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.
- laborer
- 帖子: 1016
- 注册时间: 2005-10-25 11:15
- 联系:
代码: 全选
#/bin/sh
func() {
if [ -f aa.txt ]
then
return 0;
else
return -1;
fi
}
echo "主函数开始:"
func
result="$?"
echo "result=$result"
hreiser@oakland:~$ killall -9 wife
police@oakland:~$ sudo find / -user hreiser
court@oakland:~$ sudo mv /home/hreiser /jail/
court@oakland:~$ sudo usermod -d /jail/hreiser -s "/usr/sbin/chroot /jail/" hreiser
police@oakland:~$ sudo find / -user hreiser
court@oakland:~$ sudo mv /home/hreiser /jail/
court@oakland:~$ sudo usermod -d /jail/hreiser -s "/usr/sbin/chroot /jail/" hreiser
- patrickhe
- 帖子: 681
- 注册时间: 2005-07-21 19:13
- 来自: Peking, China
- 联系: