Problem with adding date

Hi All,
Please let me know the error in the below code.

m=(0 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
M=$(date +%m)
Y=$(date +%Y)
if [ $M == 12 ] ; then 
  M=01
  Y=$(($Y+1))
else
  M=$M+1)printf "01-%s-%d\n" ${m[$M]} $Y
fi

Log:

+ m=(0 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
++ date +%m
+ M=09
++ date +%Y
+ Y=2013
+ '[' 09 == 12 ']'
/apps/powercdata/CS/TRAVEL_APPS/Scripts/payroll/gnw_Monthly_EURPayroll_ftp.sh: line 174: 09: value too great for base (error token is "09")
+ [[ 0 -ne 0 ]]

Thanks,

Are you trying to get the date for next month?

If so, then depending on your OS and version, you might be able to use something like this:-

/home/RBATTE1>date +'01-%b-%Y' -d 'next month'
01-Oct-2013

Does this neaten your code, or have I missed the point entirely?

If you cannot get this to run, but I have got the right output then we can work on your code.

I hope that this helps.

Robin
Liverpool/Blackburn
UK

Hi Robin,

Thanks for your reply. I found the issue with adding +1 to a variable.

if [ $M == 12 ] ; then 
M=01
Y=`expr $Y + 1`
else
M=`expr $M + 1`

Does that mean you have a solution?

By the way, what shell are you scripting in? I know it might seem trivial, but if you are doing lots of calculations, you could save a process by doing internal calculations rather than `expr ....

If you are ksh, for instance, you could:-

((M=$M+1))

... or a few other variations.

Robin

Leading zeroes make the shell (at least bash) assume octal values; overcome by prefixing with a base# indicator: 10#09 will be accepted.