Bash generate formatted date for last 6 months

0

I want to run a script to retrieve results from an api for each day over the last 6 months. The api endpoint uses the date as

/url/yyyy/mm/dd

How can i generate the dates using bash to acheive this?

sayth

Posted 2017-07-13T08:22:53.327

Reputation: 115

Answers

2

The following script will probably do you - although you might want to calculate 6 months ago (in seconds more exactly - I simply used 31 days * 6).

#! /bin/bash

URL="/url/"

# Key times in seconds
sixmonths=$(( 60 * 60 * 24 * 31 * 6 ))
oneday=$(( 60 * 60 * 24 ))
CURRENTSECS=`/bin/date +%s`
STARTDATEINSECS=$(( $CURRENTSECS - $sixmonths ))

i=$STARTDATEINSECS
while [ $i -le $CURRENTSECS ]
do
    echo $URL`/bin/date -d @$i +"%Y/%m/%d"`
    i=$(( $i + $oneday ))
done

davidgo

Posted 2017-07-13T08:22:53.327

Reputation: 49 152