Increment date in 'for' loop?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Increment date in 'for' loop?
# 1  
Old 10-29-2007
Question Increment date in 'for' loop?

Hi Guys,

My first post..Smilie

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
# 2  
Old 10-29-2007
Numbers statrting with 0 are evaluated as octal numbers : 070820 is an invalid octal number.

A solution is to force decimal base evaluation :
Code:
#!/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.

Last edited by aigles; 10-29-2007 at 03:03 PM..
# 3  
Old 10-29-2007
Quote:
Originally Posted by aigles
Numbers statrting with 0 are evaluated as octal numbers : 070820 is an invalid octal number.

A solution is to force decimal base evaluation :
Code:
#!/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
# 4  
Old 10-29-2007
Lets the date command increment the date for you :
Code:
#!/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.
# 5  
Old 10-30-2007
Quote:
Originally Posted by aigles
Lets the date command increment the date for you :
Code:
#!/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! :-)

You're a star!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parallel increment of nested for loop

Hi, I am using solaris 5.10 environment and need help on doing parallel increment of nested for loop. Samples #inside the code the values assigned to a variable by another awk command will be like a=/xyz/pg/as /xyz/pg/as2 /xyz/pg/as3 b=/xyz/sd/fd1 /xyz/sd/fd2 /xyz/sd/fd3 for q in... (1 Reply)
Discussion started by: ananan
1 Replies

2. Shell Programming and Scripting

How to increment date using "for loop" in format MMDDYY inside the shell script?

Need to increment the date from "currentdate + 90days" inside the for loop (i=1 to i=50) (5 Replies)
Discussion started by: aroragaurav.84
5 Replies

3. Shell Programming and Scripting

For/While Loop to Increment Filenames in a Script

Daily stupid question. I want to increment the file name everytime the script is run. So for example if the filename is manager.log and I run the script, I want the next sequence to be manager.log1. So to be clear I only want it to increment when the script is executed. So ./script... (10 Replies)
Discussion started by: metallica1973
10 Replies

4. Programming

[Xquery] How to do a increment in a For loop

Hello men. How can i build a simple increment for $a by Xquery such as ? let $a := 0 for $i in (1 to 10) let $a := $a + 1 return $a why a in this loop always is '1' Thank you for reading, its will really helpful for my job if i can solve this problem :D:D (3 Replies)
Discussion started by: tien86
3 Replies

5. Shell Programming and Scripting

how to update date part with new increment date time

hi experts, my requirement is like this i need to develop a shell script to update date part with new incremental date time in file some 'X' which is kept at some server location incrementing every two hours.as i am new to this scripting i need support from u people,thanx in advance (1 Reply)
Discussion started by: amanmro
1 Replies

6. Shell Programming and Scripting

Date increment

hi Friends, Today_Dt=`date "+%Y-%m-%d"` So the Today date is 2010-05-03 I have a file which has date values as below 2010-04-27 2010-04-02 2010-04-18 2010-04-28 2010-04-29 .. (1 Reply)
Discussion started by: Gopal_Engg
1 Replies

7. Shell Programming and Scripting

Increment in date

Hi, I have a variable lets say DATA_DATE. I have to pass some value to this variable in YYYYMMDD format. lets say today I have passed this variable as : DATA_DATE=20100107 Then pls help me how to calculate another variable DATA_DATE1 (which is DATA_DATE+1). The code should work... (3 Replies)
Discussion started by: 46019
3 Replies

8. Shell Programming and Scripting

Increment date in script

Hi, Iam new to scripting language.:o can someone help me out solving this thread?hopingly ya....:) I want to write a script which connects to db and searches the count in a table which has date column and id column. If the count is not equal to 0 then it should increment the date with the one... (4 Replies)
Discussion started by: jyothi_wipro
4 Replies

9. Shell Programming and Scripting

How to increment a user defined date value in the DATE format itself using shell script?

I need to increment a date value through shell script. Input value consist of start date and end date in DATE format of unix. For eg. I need increment a date value of 1/1/09 to 31/12/09 i.e for a whole yr. The output must look like 1/1/09 2/2/09 . . . 31/1/09 . . 1/2/09 . 28/2/09... (1 Reply)
Discussion started by: sunil087
1 Replies

10. Shell Programming and Scripting

Increment nested for loop parllely

Hi , I am trying to increment the nested for loops parellely,but i cant ,i used continue 2 but the second loop not getting increment. no1="1 6 5 4 8" no2="4 7 8 0 1" for var1 in $no1 ; do for var2 in $no2 ; do line1 line 2 line 3 continue 2 done done Please help on this (4 Replies)
Discussion started by: nmahendran
4 Replies
Login or Register to Ask a Question