The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shell Script To increment Date*HElp*** niceboykunal123 Shell Programming and Scripting 1 04-23-2008 01:05 AM
how can i read a line using the 'for' vadharah Shell Programming and Scripting 10 03-02-2008 02:26 PM
the given code goes in infinite loop and does not increment variable i mrityunjay22 Shell Programming and Scripting 6 12-26-2007 02:20 AM
Using 'for' with 2 variables? micro420 Shell Programming and Scripting 4 05-19-2007 05:02 AM
Date increment in the format "YYYYMMDD" ganapati Shell Programming and Scripting 4 08-02-2006 11:45 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-29-2007
SunnyK SunnyK is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 10
Question Increment date in 'for' loop?

Hi Guys,

My first post..

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 (permalink)  
Old 10-29-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,416
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 02:03 PM..
  #3 (permalink)  
Old 10-29-2007
SunnyK SunnyK is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 10
Quote:
Originally Posted by aigles View Post
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 (permalink)  
Old 10-29-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,416
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 (permalink)  
Old 10-30-2007
SunnyK SunnyK is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 10
Quote:
Originally Posted by aigles View Post
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!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:22 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0