Mudd
Mudd

Reputation: 3

How to make my bash script to start from past 6 months and run consecutively thereafter?

MONTH=`date '6 month ago' %Y-%m`
MONTH1=`date '5 month ago' %Y-%m`
MONTH2=`date '4 month ago' %Y-%m`
MONTH3=`date '3 month ago' %Y-%m`
MONTH4=`date '2 month ago' %Y-%m`
MONTH5=`date '1 month ago' %Y-%m`
MONTH6=`date %Y-%m`

How do i make the script run consecutively as it stops running at the current month? Start from 2013-07 till the current month 2014-01 and it keeps on adding for the consecutive months.

Thanks in advance!

Upvotes: 0

Views: 148

Answers (1)

glenn jackman
glenn jackman

Reputation: 247042

You just need a fancier invocation of date. Here's 10 months, adjust accordingly for the amount of future months you need:

day=$(date -d '6 months ago' +%F)

for ((i=0; i<10; i++)); do 
    month=${day%-*}
    echo $month
    day=$(date -d "$day + 1 month" +%F)
done
2013-07
2013-08
2013-09
2013-10
2013-11
2013-12
2014-01
2014-02
2014-03
2014-04

%F is the same as %Y-%m-%d

Upvotes: 1

Related Questions