Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


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

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-25-2013
Registered User
 
Join Date: Oct 2012
Posts: 47
Thanks: 5
Thanked 0 Times in 0 Posts
Date Range in UNIX

Hi Everyone How all are doing today,

Want some help from All in Unix, What I am trying to do is , A shell file should be a called with two date parameters suppose the shell file name is run.sh

run.sh <start_date> <end_date>
If end date is not given it will pick today's date. The date should be in format yyyy-mm-dd.

Now the twist is suppose I have given the dates

Code:
 run.sh 2012-01-01 2012-01-30

Inside this shell file It should pick the data range max of 6 days from start date like

at first hit the Start date and end date should be 2012-01-01 and 2012-01-07 #- 6days and print on the screen
at second hit the Start date and end date should be 2012-01-07 and 2012-01-13 #- 6days ..
.
.
.
.
at last hit the Start date and end date should be 2012-01-25 and 2012-01-30 #- 6days ..

I hope I am able to clear you
Thanks
Sponsored Links
    #2  
Old 01-25-2013
fpmurphy's Avatar
who?
 
Join Date: Dec 2003
Location: /dev/ph
Posts: 4,430
Thanks: 47
Thanked 358 Times in 332 Posts
Using the ksh93 shell ...

Code:
#!/bin/ksh93

nextdate=$(printf "%(%s)T" "$1")
enddate=$(printf "%(%s)T" "$2")
typeset -i DAY=24*60*60

while (( nextdate < enddate ))
do
    printf "%(%Y-%m-%d)T" "#${nextdate}"
    for ((i=1; i < 7 &&  0 == $(( (nextdate + $DAY) > enddate )); i++))
    do
       (( nextdate += $DAY ))
    done
    printf "   %(%Y-%m-%d)T\n" "#${nextdate}"
done

produces

Code:
$ ./run.sh  2012-01-01 2012-01-30
2012-01-01   2012-01-07
2012-01-07   2012-01-13
2012-01-13   2012-01-19
2012-01-19   2012-01-25
2012-01-25   2012-01-30
$

Sponsored Links
    #3  
Old 02-11-2013
Registered User
 
Join Date: Oct 2012
Posts: 47
Thanks: 5
Thanked 0 Times in 0 Posts
Hello

The code fails when I am writing

Code:
./run.sh 2013-01-01 2013-05-05

---------- Post updated at 08:26 AM ---------- Previous update was at 05:13 AM ----------

It hangs on 2013-05-04
    #4  
Old 02-11-2013
Registered User
 
Join Date: Nov 2012
Posts: 33
Thanks: 3
Thanked 1 Time in 1 Post
while (( nextdate <= enddate )) ?
Sponsored Links
    #5  
Old 02-11-2013
Yoda's Avatar
Jedi Master
 
Join Date: Jan 2012
Location: Galactic Empire
Posts: 2,309
Thanks: 154
Thanked 738 Times in 710 Posts
What is your OS uname and SHELL echo $SHELL ?
Sponsored Links
    #6  
Old 02-11-2013
Registered User
 
Join Date: Oct 2012
Posts: 47
Thanks: 5
Thanked 0 Times in 0 Posts
Linux
KSH
Sponsored Links
    #7  
Old 02-11-2013
Yoda's Avatar
Jedi Master
 
Join Date: Jan 2012
Location: Galactic Empire
Posts: 2,309
Thanks: 154
Thanked 738 Times in 710 Posts
If you have GNU date

Code:
#!/bin/bash

DT1=$1
DT2=$2

E_DT1=$( date -d"$DT1" +"%s" )
E_DT2=$( date -d"$DT2" +"%s" )

while [ $E_DT1 -lt $E_DT2 ]
do
        printf "%s " $( date -d@"$E_DT1" +"%Y-%m-%d" )
        DT1=$( date -d"$DT1 +6 days" +"%Y-%m-%d" )
        E_DT1=$( date -d"$DT1" +"%s" )
        [[ $E_DT1 -le $E_DT2 ]] && printf "%s\n" $( date -d@"$E_DT1" +"%Y-%m-%d" ) || printf "%s\n" $( date -d@"$E_DT2" +"%Y-%m-%d" )
done

---------- Post updated at 08:26 ---------- Previous update was at 08:25 ----------

Here is the o/p

Code:
$ ./dtrange 2013-01-01 2013-05-05
2013-01-01 2013-01-07
2013-01-07 2013-01-13
2013-01-13 2013-01-19
2013-01-19 2013-01-25
2013-01-25 2013-01-31
2013-01-31 2013-02-06
2013-02-06 2013-02-12
2013-02-12 2013-02-18
2013-02-18 2013-02-24
2013-02-24 2013-03-02
2013-03-02 2013-03-08
2013-03-08 2013-03-14
2013-03-14 2013-03-20
2013-03-20 2013-03-26
2013-03-26 2013-04-01
2013-04-01 2013-04-07
2013-04-07 2013-04-13
2013-04-13 2013-04-19
2013-04-19 2013-04-25
2013-04-25 2013-05-01
2013-05-01 2013-05-05

Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
date range Niven UNIX Desktop for Dummies Questions & Answers 3 02-04-2010 02:58 AM
Get date range between 2 date input tanit Shell Programming and Scripting 3 04-21-2009 11:20 AM
automated ftp script from unix -date range of files koduri0475 UNIX for Advanced & Expert Users 1 11-17-2005 01:59 PM
automated ftp script from unix -date range of files koduri0475 Programming 1 11-10-2005 09:51 AM
automated ftp script from unix -date range of files koduri0475 Shell Programming and Scripting 1 11-10-2005 09:50 AM



All times are GMT -4. The time now is 04:22 AM.