|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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}"
doneproduces 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
|
|||
|
|||
|
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
|
|||
|
|||
|
while (( nextdate <= enddate )) ?
|
| Sponsored Links | |
|
|
#5
|
||||
|
||||
|
What is your OS
uname and SHELL
echo $SHELL ?
|
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
Linux
KSH |
| Sponsored Links | |
|
|
#7
|
||||
|
||||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|