Reputation: 916
I am trying to find the previous month's year value. Below is my code. Instead of printing 2015, it is wrongly printing as 223. What could be the issue?
findPrvYear(){
echo "Finding Previous Year"
(
set `date +%m" "%Y`
CURMTHY=$1
CURYRY=$2
if [ $CURMTHY -eq 1 ]
then PRVMTHY=12
PRVYRY=`expr $CURYRY - 1`
else PRVMTHY=`expr $CURMTHY - 1`
PRVYRY=$CURYRY
fi
echo $PRVYRY //This is properly printing as 2015
return "$PRVYRY"
)
}
thisMonthY=$(date +%m)
thisYearY=$(date +%y)
findPrvYear $thisMonthY $thisYearY
retPrvYear=$?
echo $retPrvYear //This is wrongly printing as 223
Upvotes: 0
Views: 37
Reputation: 88296
You need to use $(...)
to get the output from that function. Not $?
, which gives you the return code (which is something different). So, do something more like this:
findPrvYear(){
echo "Finding Previous Year"
(
set `date +%m" "%Y`
CURMTHY=$1
CURYRY=$2
if [ $CURMTHY -eq 1 ]
then PRVMTHY=12
PRVYRY=`expr $CURYRY - 1`
else PRVMTHY=`expr $CURMTHY - 1`
PRVYRY=$CURYRY
fi
return "$PRVYRY"
)
}
thisMonthY=$(date +%m)
thisYearY=$(date +%y)
retPrvYear=$(findPrvYear $thisMonthY $thisYearY)
Upvotes: 1