用shell函数取不到返回值,为什么?

wangpqing 2007-07-17 09:09:14
代码如下:


getPrevDate(){
# Set the current month day and year.
month=`date +%m`
day=`date +%d`
year=`date +%Y`

# Add 0 to month. This is a
# trick to make month an unpadded integer.
month=`expr $month + 0`

# Subtract $1 from the current day.
day=`expr $day - $1`

# If the day is 0 or less than 0 then determine the last
# day of the previous month.
if [ $day -eq 0 -o $day -lt 0 ]; then

# Find the preivous month.
month=`expr $month - 1`

# If the month is 0 then it is Dec 31 of
# the previous year.
if [ $month -eq 0 ]; then
month=12
day=31
year=`expr $year - 1`

# If the month is not zero we need to find
# the last day of the month.
else
case $month in
1|3|5|7|8|10|12) day=`expr 31 + $day`;;
4|6|9|11) day=`expr 30 + $day`;;
2)
if [ `expr $year % 4` -eq 0 ]; then
if [ `expr $year % 400` -eq 0 ]; then
day=`expr 29 + $day`
elif [ `expr $year % 100` -eq 0 ]; then
day=`expr 28 + $day`
else
day=`expr 29 + $day`
fi
else
day=`expr 28 + $day`
fi
;;
esac
fi
fi

# Print the month day and year.
case $month in 1|2|3|4|5|6|7|8|9) month=0$month ;;
esac
case $day in 1|2|3|4|5|6|7|8|9) day=0$day ;;
esac
result=$year$month$day
echo $result
return $result
}

#--main----------------------------------------
echo testing the funciton getPrevDate start:
getPrevDate 1
resp=$?
echo resp is : $resp

问题描述:
在函数里面 echo $result,可以正常显示如:20070716
但是在后面resp=$?却返回60?
不知道为啥,
烦劳高人指点。先谢了
...全文
744 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ni29724244801 2009-09-15
  • 打赏
  • 举报
回复
getPrevDate(){
# Set the current month day and year.
month=`date +%m`
day=`date +%d`
year=`date +%Y`

# Add 0 to month. This is a
# trick to make month an unpadded integer.
month=`expr $month + 0`

# Subtract $1 from the current day.
day=`expr $day - $1`

# If the day is 0 or less than 0 then determine the last
# day of the previous month.
if [ $day -eq 0 -o $day -lt 0 ]; then

# Find the preivous month.
month=`expr $month - 1`

# If the month is 0 then it is Dec 31 of
# the previous year.
if [ $month -eq 0 ]; then
month=12
day=31
year=`expr $year - 1`

# If the month is not zero we need to find
# the last day of the month.
else
case $month in
1|3|5|7|8|10|12) day=`expr 31 + $day`;;
4|6|9|11) day=`expr 30 + $day`;;
2)
if [ `expr $year % 4` -eq 0 ]; then
if [ `expr $year % 400` -eq 0 ]; then
day=`expr 29 + $day`
elif [ `expr $year % 100` -eq 0 ]; then
day=`expr 28 + $day`
else
day=`expr 29 + $day`
fi
else
day=`expr 28 + $day`
fi
;;
esac
fi
fi

# Print the month day and year.
case $month in 1|2|3|4|5|6|7|8|9) month=0$month ;;
esac
case $day in 1|2|3|4|5|6|7|8|9) day=0$day ;;
esac
result=$year$month$day
echo $result
# return $result
}

#--main----------------------------------------
echo testing the funciton getPrevDate start:
var=`getPrevDate 1`
#resp=$?
echo resp is : $var
这是我改过的代码
把 getPrevDate函数里面的return $result 去掉
在调用getPrevDate 函数的地方修改成 var=`getPrevDate 1`
这是echo返回值的方法就可以返回 “20070716”这个值
wangpqing 2007-07-25
  • 打赏
  • 举报
回复
谢谢cceczjxy
返回去再看资料,终于明白了。
wangpqing 2007-07-23
  • 打赏
  • 举报
回复
等明天测试完毕,回来发钱︿_︿
cceczjxy 2007-07-20
  • 打赏
  • 举报
回复
写错了,该这样
resp=`getPrevDate 1`
cceczjxy 2007-07-20
  • 打赏
  • 举报
回复
$?是取此命令exit的返回值,不是取函数的返回值.

---------------------------
我记错了,$?是取上一个命令的返回值.

是可以用$?取函数的返回值,不过返回值大小不能超过255,且需是numeric型

如果其他类型的返回值可以这样取


resp=getPrevDate 1

resp将取到getPrevDate函数内第一个echo命令输出的值.
wangpqing 2007-07-19
  • 打赏
  • 举报
回复
哦,明白了。
今天换了种方式,就是先在函数外设一变量,然后在函数里修改该变量值即可。
但是如果要取到函数的返回值,应该怎么样呢?
我查了很多地方,都讲的含糊。
是不是shell中的return和其他语言中的return差别很大?

cceczjxy 2007-07-19
  • 打赏
  • 举报
回复
$?是取此命令exit的返回值,不是取函数的返回值.
cceczjxy 2007-07-18
  • 打赏
  • 举报
回复
testing the funciton getPrevDate start:
20070717
resp is : 61



我这可以呀
wangpqing 2007-07-18
  • 打赏
  • 举报
回复
对,就是那个61。
返回不应该是:20070717 吗?
上面已经把 $? 赋给 resp 了啊?
wangpqing 2007-07-17
  • 打赏
  • 举报
回复
还不是很明白。
我上面的代码里哪里给引用错误了?还是如何?
我试验过一个求2个数和的函数,和上面的写法一样,$?就可以取道正确的值啊
所以还不是很明白
lsyer 2007-07-17
  • 打赏
  • 举报
回复
$? 由shell自动设置为最后执行的命令的退出状态
$# 为上一个运行的命令
wangpqing 2007-07-17
  • 打赏
  • 举报
回复
没人顶??
自己顶先

23,125

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧