To run the script based on dates

I am having below script which needs to be executed based on start and end date

#!/bin/bash
array=('2016-09-27' '2016-10-27' '2016-11-27' '2016-12-27' '2017-01-27' '2017-02-27' '2017-03-27' '2017-04-27' '2017-05-27' '2017-06-27' '2017-07-27' '2017-08-27' '2017-09-27' )
for i in "${array[@]}"
do
echo "executing" $i

sh test.sh $i

if [ "$?" = "0" ]; then
echo "completed $i"
else
echo "failed"
#exit 1
fi

If i pass parameter as 2017-09-27 and 12, then the script should loop for last 12 months including 2017-09-27, Similarly if i give 6 then 6 months likewise any number

So far i have tried

#!/bin/bash
FROM_DATE=2016-09-27
TO_DATE=2017-09-27
FROM_DT=$FROM_DATE
while [[ $FROM_DATE < $TO_DATE ]]
do

echo "executing for $FROM_DT"

sh  test $FROM_DT

if [ "$?" = "0" ]; then
echo "completed $FROM_DT"
else
echo "failed"
#exit 1
fi
FROM_DATE=$(date -d "$FROM_DATE + 1 month" +"%Y-%m-%d")

done

Not sure I understand. You seem to want to run a script for several selectable months back from a certain date.
Why do you define that 12 element array? Why the loop? Where do you pass the date parameter?

Did you search these forums for similar if not idential problems and solutions? I'm pretty sure that kind of problem has been solved umpteen times before.

Actually Yes i will pass in my script the date and duration like say 12 or 6 or anything based on that it will loop the internal script 12 months by passing tha parameter


sh main_script.sh 2017-09-27 12

#this will loop like this

sh test.sh 2016-09-27
sh test.sh 2016-10-27

.
.
.
.

But what is your problem? Eyeballing your script suggests to me that it should work the way you intend it to. Your original post boils down to "I want to do this and have written this script to do it". A statement and no questions.

Andrew

Should your problem be that the date parameter to test.sh is not progressing, you might want to scrutinize sh test $FROM_DT and its development...

Sorry for confusing guys. Actually as i said i am having a main script where date and number of months will be passed fo e.g..

sh main_script.sh 2017-12-10 12<YYYY-MM-DD>

Inside the main script i would be calling a subscript called test.sh which has date as input parameter. Based on my inputs from main_script parameter i need to run the internal script for 12 months back starting from 2016-12-10 till 2017-12-10 by passing each dates to test.sh

sh test.sh 2016-09-27
sh test.sh 2016-10-27

.
.
.
.

What is that <YYYY-MM-DD> ?

Oh thats i was referring to which format the dates are being passed basically YYYY-MM-DD

sh main_script.sh 2017-12-10 12

How about

FDT=$(date -d "$1 - 12 month" +"%Y-%m-%d")
for MP in $(seq 0 $2); do date -d "$FDT + $MP month" +"%Y-%m-%d"; done
2016-12-10
2017-01-10
2017-02-10
2017-03-10
2017-04-10
2017-05-10
2017-06-10
2017-07-10
2017-08-10
2017-09-10
2017-10-10
2017-11-10
2017-12-10

Be aware that this yields 13 iterations, for 12 months back and the current month.

Thanks,

it worked for me