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?

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.