Repeating loop between dates


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Repeating loop between dates
# 1  
Old 11-01-2010
Repeating loop between dates

Hi ,

I need to execute set of commands between two parameterized dates.

Suppose, If parameter1 is Feb1st-2010 and parameter2 is November15th-2010. I need to execute a set of commands within these dates .
can any one help me to build a loop so that it should execute for 28days in February and 31 days in march likewise..
# 2  
Old 11-01-2010
If you have GNU date (ie date supports -d) using bash (note 86400 is number of seconds in a day 24*60*60):

Code:
#!/bin/bash
START=$(date -d 1-Feb-2010 +%s )
END=$(date -d 15-Nov-2010 +%s )
for((i=START; i<=END; i+=86400))
do
    date -d @$i
done

# 3  
Old 11-02-2010
Chubler thanks for Quick reply.
I have one more query ,I need to print the date for each iteration in the for loop.
i.e. for the 1st iteration the date should be returned as 01/02/2010 and for the 2nd iteration it is returned as 01/02/2010 likewise ..

Thanks,
# 4  
Old 11-02-2010
My original code prints the date for each iteration:

Code:
Mon Feb  1 00:00:00 EAST 2010
Tue Feb  2 00:00:00 EAST 2010
...

If you want dd/mm/yyyy format, use +%d/%m/%Y in the loop:

Code:
#!/bin/bash
START=$(date -d 1-Feb-2010 +%s )
END=$(date -d 15-Nov-2010 +%s )
for((i=START; i<=END; i+=86400))
do
    date -d @$i +%d/%m/%Y
done

# 5  
Old 11-02-2010
thank you very much
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Display dates between two dates

Hi All, I have 2 dates in mm/dd format. sdate=10/01 (October 01) edate=10/10 (October 10) I need the dates in between these 2 dates like below. 10/01 10/02 10/03 10/04 10/05 10/06 10/07 10/08 (1 Reply)
Discussion started by: jayadanabalan
1 Replies

2. Shell Programming and Scripting

Shell Script to Loop through Quarter dates

Hi, Trying to automate a Postgres query using Shell script Every month 1st week has to run a Monthly Queries to generate delimited files. July 1st start of fiscal yr which has 4 Quarters until next June 30th Example If I'm running on Sept 5th it has to generate one file(Becuase it... (12 Replies)
Discussion started by: krux_rap
12 Replies

3. UNIX for Advanced & Expert Users

How to get the Missing dates between two dates in the table?

Hi Am Using Unix Ksh ... I have a Table called date select * from date ; Date 01/02/2013 06/02/2013 I need the output as Missing Date 01/02/2013 02/02/2013 03/02/2013 04/02/2013 05/02/2013 06/02/2013 (2 Replies)
Discussion started by: Venkatesh1
2 Replies

4. Shell Programming and Scripting

Generating dates between two dates

HI, i have row like this HHH100037440313438961000201001012012073110220002 N in this i have 2 dates in pos 25-32 and 33-40 , so based upon the se two dates , i need to generated records between these two values so in the above record 20100101 and 20120731 need to genearte rows like this... (4 Replies)
Discussion started by: sathishsr
4 Replies

5. UNIX for Dummies Questions & Answers

How to write the dates between 2 dates into a file

Hi All, I am trying to print the dates that falls between 2 date variables into a file. Here is the example. $BUS_DATE =20120616 $SUB_DATE=20120613 Output to file abc.txt should be : 20120613,20120614,120120615,20120616 Can you pls help me accomplish this in LINUX. Thanks... (5 Replies)
Discussion started by: dsfreddie
5 Replies

6. Emergency UNIX and Linux Support

Replacing dates]] with (dates)]]

Hi guys, For my wiki site I need to fix 1400 pages that use the wrong date format, most pages (not all) use eg. 1988]] I need to change that to (1988)]] The date range goes back to 1400 so I guess I need to do the following ssh into my server, dump mysql database vi .sql dump search... (20 Replies)
Discussion started by: lawstudent
20 Replies

7. Programming

SQL: find if a set od dates falls in another set of dates

Don't know if it is important: Debian Linux / MySQL 5.1 I have a table: media_id int(8) group_id int(8) type_id int(8) expiration date start date cust_id int(8) num_runs int(8) preferred_time int(8) edit_date timestamp ON UPDATE CURRENT_TIMESTAMP id... (0 Replies)
Discussion started by: vertical98
0 Replies

8. Shell Programming and Scripting

Need script to generate all the dates in DDMMYY format between 2 dates

Hello friends, I am looking for a script or method that can display all the dates between any 2 given dates. Input: Date 1 290109 Date 2 010209 Output: 300109 310109 Please help me. Thanks. :):confused: (2 Replies)
Discussion started by: frozensmilz
2 Replies

9. Shell Programming and Scripting

Repeating variables in the code

Hi all, I had written 3 KSH scripts for different functionalities. In all these 3 files there are some 30 variables in common. So I want to reduce the code by placing these variables in a common properties file named (dataload.prop/dataload.parms/dataload.txt) or txt file and access it... (1 Reply)
Discussion started by: mahalakshmi
1 Replies

10. UNIX for Advanced & Expert Users

repeating kernel message

Hi, I am getting multiple repeats of the following message in /var/log/messages Jul 26 13:36:04 linuxnol kernel: cdrom: open failed. Jul 26 13:36:11 linuxnol kernel: cdrom: open failed. Jul 26 13:38:18 linuxnol kernel: cdrom: open failed. Jul 26 13:38:26 linuxnol kernel: cdrom: open... (2 Replies)
Discussion started by: progressdll
2 Replies
Login or Register to Ask a Question