Call script parameter based on dates


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Call script parameter based on dates
# 1  
Old 05-15-2018
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.

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

sh invoke_script var_d

i need to iterate the inside script something like below

Code:
sh invoke_script 170101
sh invoke_script 170102
sh invoke_script 170103
.
.
.
.
sh invoke_script 171231

I will pass my main_script two parameters
Code:
sh main_script 170101 12

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

Tried so far
Code:
#!/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


Last edited by Master_Mind; 05-15-2018 at 03:15 AM.. Reason: Adding comments
# 2  
Old 05-15-2018
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?

Last edited by RudiC; 05-15-2018 at 09:31 AM..
# 3  
Old 05-15-2018
Let me rephrase the problem.
I need to invoke the subscript based on no of months passed and from which date i pass

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

# 4  
Old 05-15-2018
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):
Code:
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.
# 5  
Old 05-15-2018
Thanks Rudic,

Can this be done in this way. I mean similar to this
Code:
var_d=$1
months=$2

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


Last edited by Master_Mind; 05-15-2018 at 10:43 AM.. Reason: comments
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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... (9 Replies)
Discussion started by: rohit_shinez
9 Replies

2. Shell Programming and Scripting

awk script to call another script based on second column entry

Hi I have a text file (Input.txt) with two column entries separated by tab as given below: aaa str1 bbb str2 cccccc str3 dddd str4 eee str3 ssss str2 sdf str3 hhh str1 fff str2 ccc str3 ..... ..... ..... (1 Reply)
Discussion started by: my_Perl
1 Replies

3. Shell Programming and Scripting

To call Oracle procedure by reading parameter file in UNIX

hi Guys, is there a way to pass parameter into oracle store procedure by reading date range in file and increment accordingly. Something like this file.txt 01-JAN-2015 03-JAN-2015 sqlplus -s user/pwd@DB execute TEST( to_date( '01-JAN-2015, 'dd.mm.yyyy' ), to_date( '03-JAN-2015', ... (1 Reply)
Discussion started by: rohit_shinez
1 Replies

4. Shell Programming and Scripting

Call Script with Parameter (that has another parameter)

Hi. How do I achieve this sh /EDWH-DMT02/script/MISC/exec_sql.sh "@/EDWH-DMT02/script/others/CSM_CKC/Complete_List.sql ${file_name}" Complete_List.txt The /EDWH-DMT02/script/MISC/exec_sql.sh has two parameters and it's working fine with this sh /EDWH-DMT02/script/MISC/exec_sql.sh... (7 Replies)
Discussion started by: aimy
7 Replies

5. Shell Programming and Scripting

help with script parameter checking based on environment

I need to check if the parameters are correctly passed based on the Environment I am in. For e.g when I am in dev the 1st paramter needs to be either A OR B OR C OR D similarly when I am in qa the parameter needs to be either e or f so i need to write a case staement or a if statement to... (1 Reply)
Discussion started by: dsravan
1 Replies

6. Shell Programming and Scripting

How to call stored procedure with CLOB out parameter from shell script?

I have written a stored procedure in oracle database, which is having a CLOB OUT parameter. How can i call this stored procedure from shell script and get the CLOB object in shell script variable? (0 Replies)
Discussion started by: vel4ever
0 Replies

7. AIX

Audit problem : A system call received a parameter that is not valid.

while i try to start the audit i have the below error message . audit>audit start ** auditing enabled already A system call received a parameter that is not valid. please advice (6 Replies)
Discussion started by: thecobra151
6 Replies

8. Shell Programming and Scripting

script to Find the files based on dates it came

I need a script to get the files for DEC the file format will be B20_abc_ancdfefg_20101209_INCR_201012100145.TXT.gz Based on the bold ones (date) i need to get the files can any one help me plzzzzzz (2 Replies)
Discussion started by: krux_rap
2 Replies

9. Shell Programming and Scripting

Not getting the out value parameter of a DB call in the ksh file

Hi all Im calling a DB procedure as foll sqlplus -s $DB_USERID/$DB_PASSWD@$DB_NAME<<eof var var1 VARCHAR2(200); exec ODAS_BATCH_JOBS_RETRIEVE.retrieve_user_info(:var1); eof echo $var1 This echo is giving a blank. Also in case the package ODAS_BATCH_JOBS_RETRIEVE is in an un compiled... (2 Replies)
Discussion started by: Sam123
2 Replies

10. Shell Programming and Scripting

Need script to generate all the dates in DDMMYY format between 2 dates

Hello friends, I am looking for a script or method that can display all the dates between any 2 given dates. Input: Date 1 290109 Date 2 010209 Output: 300109 310109 Please help me. Thanks. :):confused: (2 Replies)
Discussion started by: frozensmilz
2 Replies
Login or Register to Ask a Question