Increment date in 'for' loop?

Hi Guys,

My first post..:slight_smile:

Right...I want to move existing files (with some date in their name) currently in $mainftp, to $mainfolder/$foldate/system1.

I'd like to be able to increment date in the for loop? Is this possible or should I use a different technique.

The script return the following error

./movediselogs.sh: line 8: ((: foldate=070820: value too great for base (error token is "070820")

Hope that makes sense...

-----------------------

#!/bin/sh

startdate=`/bin/date --date="10 weeks ago" +%y%m%d`
currentdate=`/bin/date +%y%m%d`
mainfolder=/data/backup
mainftp=/ftp/upload/ftpsystem1

for ((foldate=$startdate; $foldate < $currentdate; foldate++))
do
mkdir $mainfolder/$foldate/system1
mv $mainftp/*$foldate* $mainfolder/$foldate/system1
done

-----------------------

Many Thanks
SunnyK

Numbers statrting with 0 are evaluated as octal numbers : 070820 is an invalid octal number.

A solution is to force decimal base evaluation :

#!/bin/sh

startdate=10#`/bin/date --date="10 weeks ago" +%y%m%d`
currentdate=10#`/bin/date +%y%m%d`
mainfolder=/data/backup
mainftp=/ftp/upload/ftpsystem1

for ((foldate=$startdate; $foldate < $currentdate; foldate++))
do
   mkdir $mainfolder/$foldate/system1
   mv $mainftp/*$foldate* $mainfolder/$foldate/system1
done

This script will not give you any error, but i am not sure that the result is what you are expected.
Incrementng the date will give you invalid dates, for example :
070831+1=070832 <= Invalid date
Jean-Pierre.

Hey Jean,

Thanks for replying...

The solution you suggested should help me accomplish my task until date reaches the value 070831.

However, after that, I'd like the script to increment the month (as in performing increment on a date variable) and perform the remaining part of the task until currentdate.

I think this shouldnt be much of a problem in other programming languages (I am new to shell scripting though). Also, if you feel there is a better alternate approach, please feel free to suggest.

Thanks
SunnyK

Lets the date command increment the date for you :

#!/bin/sh

startdate=`/bin/date --date="10 weeks ago" +%y%m%d`
currentdate=`/bin/date +%y%m%d`
mainfolder=/data/backup
mainftp=/ftp/upload/ftpsystem1

foldate="$startdate"
until [ "$foldate" == "$currentdate" ]
do
   mkdir $mainfolder/$foldate/system1
   mv $mainftp/*$foldate* $mainfolder/$foldate/system1
   foldate=`/bin/date --date="$foldate 1 day" +%y%m%d`
done

Jean-Pierre.

Thanks Jean...it works! :slight_smile:

You're a star!