Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, I am new to linux and shell scripting. I have a requirement where I need to write to a file a block of static text for each month starting ...
  1. #1
    Just Joined!
    Join Date
    Dec 2010
    Posts
    2

    How to get all month start and end date between two dates?

    Hi,
    I am new to linux and shell scripting. I have a requirement where I need to write to a file a block of static text for each month starting from 01-01-2000. It would be something like below

    XXXXXXXXXXXXXXXXXXXXX BETWEEN 2000-01-01 AND 2000-01-31 XXXXXX


    XXXXXXXXXXXXXXXXXXXXX BETWEEN 2000-02-01 AND 2000-02-29 XXXXXX


    XXXXXXXXXXXXXXXXXXXXX BETWEEN 2000-03-01 AND 2000-03-31 XXXXXX

    ........

    XXXXXXXXXXXXXXXXXXXXX BETWEEN 2010-12-01 AND 2010-12-31 XXXXXX



    The XXXXX all are some static text. The final block dates 2010-12-01 and 2010-12-31 needs to be constructed from a parameter passed to the shell script.

    I was able to create a loop and write each block to the file but I dont have any idea how to get the start date and end date of each month and how to increment the months.
    Can anyone provide some idea how to do it?

  2. #2
    Just Joined!
    Join Date
    Dec 2010
    Location
    India
    Posts
    45
    i have a piece of code which calculates the start and end date of month
    u can modify that according to ur requirement.

    month=1
    year=2009
    while [ $year -le 2010 ]
    do
    if [ $month -le 12 ]
    then
    date --d "`date`" "+01-${month}-$year"
    nMonth=`expr $month + 1`
    date --d "`date +${nMonth}/01/$year` yesterday" "+%d-${month}-$year"
    month=$nMonth
    else
    year=`expr $year + 1`
    month=1
    fi
    done

  3. #3
    Just Joined!
    Join Date
    Dec 2010
    Posts
    2
    Hi Piyu,
    Thanks a lot. this was really the stuff what I wanted. Could you please also show me how to assign the month end date to a variable so that I can concatenate it with my text. I could assign the month start date to a varibale but not month end date as it's calculation is a little complex for me.



    #!/bin/bash
    month=1
    year=2009
    startdate=""
    enddate=""
    while [ $year -le 2010 ]
    do
    if [ $month -le 12 ]
    then
    startdate=01-${month}-$year
    nMonth=`expr $month + 1`
    echo "Select * from tbl_name where date between" ${startdate} "and "
    date --d "`date +${nMonth}/01/$year` yesterday" "+%d-${month}-$year"
    month=$nMonth
    else
    year=`expr $year + 1`
    month=1
    fi
    done

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...