Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

To run the script based on dates

Shell Programming and Scripting


Tags
date -d, solved

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 5 Days Ago   -   Original Discussion by rohit_shinez
rohit_shinez's Unix or Linux Image
rohit_shinez rohit_shinez is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 8 December 2017, 2:18 AM EST
Posts: 192
Thanks: 59
Thanked 0 Times in 0 Posts
To run the script based on dates

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

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

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

Sponsored Links
    #2  
Old Unix and Linux 4 Days Ago   -   Original Discussion by rohit_shinez
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 11 December 2017, 3:37 PM EST
Location: Aachen, Germany
Posts: 11,780
Thanks: 339
Thanked 3,659 Times in 3,357 Posts
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 fora for similar if not idential problems and solutions? I'm pretty sure that kind of problem has been solved umpteen times before.
Sponsored Links
    #3  
Old Unix and Linux 4 Days Ago   -   Original Discussion by rohit_shinez
rohit_shinez's Unix or Linux Image
rohit_shinez rohit_shinez is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 8 December 2017, 2:18 AM EST
Posts: 192
Thanks: 59
Thanked 0 Times in 0 Posts
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


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

.
.
.
.

    #4  
Old Unix and Linux 4 Days Ago   -   Original Discussion by rohit_shinez
apmcd47's Unix or Linux Image
apmcd47 apmcd47 is online now
Registered User
 
Join Date: Feb 2011
Last Activity: 12 December 2017, 5:01 AM EST
Posts: 283
Thanks: 13
Thanked 76 Times in 73 Posts
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
Sponsored Links
    #5  
Old Unix and Linux 4 Days Ago   -   Original Discussion by rohit_shinez
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 11 December 2017, 3:37 PM EST
Location: Aachen, Germany
Posts: 11,780
Thanks: 339
Thanked 3,659 Times in 3,357 Posts
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...
Sponsored Links
    #6  
Old Unix and Linux 4 Days Ago   -   Original Discussion by rohit_shinez
rohit_shinez's Unix or Linux Image
rohit_shinez rohit_shinez is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 8 December 2017, 2:18 AM EST
Posts: 192
Thanks: 59
Thanked 0 Times in 0 Posts
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..


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


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

.
.
.
.

Sponsored Links
    #7  
Old Unix and Linux 4 Days Ago   -   Original Discussion by rohit_shinez
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 11 December 2017, 3:37 PM EST
Location: Aachen, Germany
Posts: 11,780
Thanks: 339
Thanked 3,659 Times in 3,357 Posts
What is that <YYYY-MM-DD>?
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Script to read a log file and run 2nd script if the dates match SarwalR Shell Programming and Scripting 1 09-18-2012 12:46 PM
To run a script based on the value in text file nani1984 Shell Programming and Scripting 7 12-29-2010 09:37 PM
script to Find the files based on dates it came krux_rap Shell Programming and Scripting 2 12-10-2010 08:54 PM
Run a script based on the subject line of the email satekn Shell Programming and Scripting 5 04-16-2009 01:11 PM
trigger a script based on the run status of another scipt Athena Shell Programming and Scripting 1 10-02-2008 03:11 PM



All times are GMT -4. The time now is 06:05 AM.