fetch dates for last 36 days in format yyyy-mm-dd

can anyone please suggest me some ideas for writing a korn shell script which will go back to 36 days from current day and print each day (incremented by 1) in the format yyyy-mm-dd until the current day.

Thanks
Mark

There are a number of articles regarding date arithmetic on the site:

try http://www.unix.com/answers-frequently-asked-questions/13785-yesterdays-date-date-arithmetic.html

from the FAQs - you may find your answer (or something close) in there.

What have you tried so far ? Post any code you may have and you're likely to get some good assistance.

Cheers

This is the code i have so far. for now the loop is for 5 days. later i wish to update it to 36 days.

#!/bin/ksh

set -x
i=1
 while [ $i -le 5 ]; do
_day=`date +%Y%m%d`
newday=`expr $_day - $i`
echo $newday
echo "================================="
i=`expr $i + 1`
echo $i
done

but i have a problem, expr 20091001 - 2 then i see this newday=20090999

Can anyone please suggest me.

Thanks
Mark

The previously suggested FAQ link and the forum 'Search' capability should give you a head start.
Have you looked into those?

---------- Post updated at 12:05 PM ---------- Previous update was at 12:05 PM ----------

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

heres a simple way:-

#!/bin/ksh
set -x
i=1
 while [ $i -le 5 ]; do
date -d "`date +%Y`-01-01 +$(( `date +"%j"` - $i ))days" +%Y-%m-%d
i=`expr $i + 1`
echo $i
done

i have used a simple trick to convet the current calender date into julian date, substract it by 1, and convert back it to the calendar date :slight_smile:

one another way,

date --date="36 days ago"  +"%Y-%m-%d"

Use a variable and reduce the 36 upto required num, and proceed.

As, I have no experience in writing korn shell script !!! Write the above suggested by your own.

the geek -

Your method is good as long as the OP has GNU date installed on his machine.

You can also try perl.

#!/bin/ksh
# will run on Solaris with the date/time format %s problem.

end=$( perl -e ' print time;' )
start=$(( end - ( 86400 * 36) ))  # 36 days in the past
while [[  $end -ge $start  ]]
do
     perl -e 'use POSIX qw(strftime);
              $mt = strftime "%a %b %e %H:%M:%S %Y", localtime($ARGV[0]); 
              print $mt,"\n";' $start
     start=$(( $start + 86400 ))
done

DAYZ=36; for num in $(seq 0 ${DAYZ}); do date -d "-${num} day" +"%Y-%m-%d" ; done