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):