Call script parameter based on dates

Hi Guys,

I am having a script which needs to be iterated based on date passed and itrate based on no of months given.

#!/bin/bash
var_d=$1
months=$2

sh invoke_script var_d

i need to iterate the inside script something like below

sh invoke_script 170101
sh invoke_script 170102
sh invoke_script 170103
.
.
.
.
sh invoke_script 171231

I will pass my main_script two parameters

sh main_script 170101 12

sh main_script <date in YYMMDD> <No.of Months>

Tried so far

#!/bin/bash
var_d=$1
months=$2
dt=$(date -d "$1 - 12 month" +"%Y%m%d")

for i in $(months)
do
sh invoke_script $dt
done

You are using the script's parameters inconsistently, or you're describing the problem incompletely, or I'm getting it incorrectly.
Please take a step back and rephrase your problem. Do you always need 365 (366) iterations? Always 12 months? Always 12 months back? Where does $2 come into play? How does the start date relate to the parameters? Why does your loop iterate across months but supplies a constant parameter to invoke_script ? Why do you use "command substitution" to evaluate months ?

Let me rephrase the problem.
I need to invoke the subscript based on no of months passed and from which date i pass


main_script 170101 12

This means starting from 170101 need to iterate for 365 days since 12 is passed. If i pass 2 then starting from 170101 61 days i.e. 2 months

sh invoke_script 170101
sh invoke_script 170102
.
.
sh invoke_script 171231

Date & time arithmetics is one of the worst problems in IT, so you can spend endless time and resources to get it right. Ignoring several itches like short months, year end crossing, and leap years, here is a veeery simple approach to get you started with the sample data that you gave. Error messages are sent to the null device, and we use the (dangerous and deprecated) eval command because we (hope we) know exactly what we're doing, and take advantage of recent bash 's functionality. Try (with $1 set to 170101, and $2 to 12):

eval echo ${1:0:2}{${1:2:2}..$((${1:2:2}+$2-1))}{${1:4:2}..31} | tr ' ' '\n' | date -f- +'invoke_script %y%m%d' 2>/dev/null
invoke_script 170101
invoke_script 170102
invoke_script 170103
invoke_script 170104
invoke_script 170105
.
.
.
invoke_script 170227
invoke_script 170228
invoke_script 170301
invoke_script 170302
.
.
.
invoke_script 171229
invoke_script 171230
invoke_script 171231

If happy, you can run this through e.g. sh .

Thanks Rudic,

Can this be done in this way. I mean similar to this

var_d=$1
months=$2

for i in {0..$months}
do
   inc_date=$(date +%y%m%d -d "$var_d + $i day")
 
done