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.
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
.
.
.
.