当前位置: 技术问答>linux和unix
用shell函数取不到返回值,为什么?
来源: 互联网 发布时间:2016-02-27
本文导语: 代码如下: 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 +...
代码如下:
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?
不知道为啥,
烦劳高人指点。先谢了
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?
不知道为啥,
烦劳高人指点。先谢了
|
testing the funciton getPrevDate start:
20070717
resp is : 61
我这可以呀
20070717
resp is : 61
我这可以呀
|
$? 由shell自动设置为最后执行的命令的退出状态
$# 为上一个运行的命令
$# 为上一个运行的命令