Could not able to get the correct dates.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Could not able to get the correct dates.
# 1  
Old 03-15-2010
Could not able to get the correct dates.

Hi,

I have written the following script which will provide the first date of the 3rd previous month and last date of the 2nd previous month but when I change the month for 03 (March) then I am getting the incorrect results.

Ex : - If today is 1 Jan 2010 then the script should provide the 01-Oct-2009 and 30-Nov-2009,
if the today is 1 Feb 2010 then the script should provide the 1-Nov-2009 and 31-Dec-2009,
if today is 1 Mar 2010 then the scrupt should provide the 1 Dec 2009 and 31 Jan 2010,
if today is 1 st Apr then the script should provide the 1-jan-2010 and 28-Feb2010.

In the above scenarios my scruipt is working file except for the month of March...it is not providing the correct year, could you please help me on this.

day=`date '+%d'`
month=1
#month=`date '+%m'`
year=`date '+%Y'`

file_name=' '
file_name_1=' '
file_cnt=0
file_cnt_1=0

echo
echo "Today is ---> "$day'-'$month'-'$year
echo

lmonth=`expr $month - 2`

if test "$lmonth" -le "0"; then
lmonth=`expr $month + 10`
year=`expr $year - 1`
fi

lday=`cal $lmonth $year |awk '$0~/[0-9]/ {print $NF}'|tail -1`

v_3rd_last_month=`expr $lmonth - 1`

if test "$v_3rd_last_month" -le "0"; then
v_3rd_last_month=`expr $lmonth + 11`
year=`expr $year - 1`
fi

echo "First day of the 3rd last month ---> " 01'-'$v_3rd_last_month'-'$year
echo
echo "Last day of 2nd last month was ---> " $lday'-'$lmonth'-'$year
echo


OutPuts : -

If I update the month = 1 then

bash-3.00$ check_date3.sh

Today is ---> 15-1-2010

First day of the 3rd last month ---> 01-10-2009

Last day of 2nd last month was ---> 30-11-2009

bash-3.00$

If I update the month = 2 then

bash-3.00$ check_date3.sh

Today is ---> 15-2-2010

First day of the 3rd last month ---> 01-11-2009

Last day of 2nd last month was ---> 31-12-2009

bash-3.00$

If I update the month = 3 then

bash-3.00$ check_date3.sh

Today is ---> 15-3-2010

First day of the 3rd last month ---> 01-12-2009

Last day of 2nd last month was ---> 31-1-2009

bash-3.00$

If I update the month = 4 then

bash-3.00$ check_date3.sh

Today is ---> 15-4-2010

First day of the 3rd last month ---> 01-1-2010

Last day of 2nd last month was ---> 28-2-2010

bash-3.00$

In the above, the outputs for the March month's last day of 2nd last month was shosing wrong...COuld you please help me. especially year!!!!
kandi.reddy
# 2  
Old 03-15-2010
For a start,
Code:
echo "First day of the 3rd last month ---> " 01'-'$v_3rd_last_month'-'$year
echo
echo "Last day of 2nd last month was ---> " $lday'-'$lmonth'-'$year

$year in the second echo statement should not be the same variable as the first.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Display dates between two dates

Hi All, I have 2 dates in mm/dd format. sdate=10/01 (October 01) edate=10/10 (October 10) I need the dates in between these 2 dates like below. 10/01 10/02 10/03 10/04 10/05 10/06 10/07 10/08 (1 Reply)
Discussion started by: jayadanabalan
1 Replies

2. Programming

Get correct mail id

Hi Team, I have below data , could you please help to get correct mail id in oracle only. bhatt,rabi rabi.bhatt@n.com, test, mishra test.mishra@n.com, skype, amit skype.amit@n.com, output like :rabi.bhatt@n.com test.mishra@n.com skype.amit@n.com Please use CODE tags as required... (6 Replies)
Discussion started by: Jewel
6 Replies

3. UNIX for Advanced & Expert Users

How to get the Missing dates between two dates in the table?

Hi Am Using Unix Ksh ... I have a Table called date select * from date ; Date 01/02/2013 06/02/2013 I need the output as Missing Date 01/02/2013 02/02/2013 03/02/2013 04/02/2013 05/02/2013 06/02/2013 (2 Replies)
Discussion started by: Venkatesh1
2 Replies

4. Shell Programming and Scripting

Generating dates between two dates

HI, i have row like this HHH100037440313438961000201001012012073110220002 N in this i have 2 dates in pos 25-32 and 33-40 , so based upon the se two dates , i need to generated records between these two values so in the above record 20100101 and 20120731 need to genearte rows like this... (4 Replies)
Discussion started by: sathishsr
4 Replies

5. UNIX for Dummies Questions & Answers

How to write the dates between 2 dates into a file

Hi All, I am trying to print the dates that falls between 2 date variables into a file. Here is the example. $BUS_DATE =20120616 $SUB_DATE=20120613 Output to file abc.txt should be : 20120613,20120614,120120615,20120616 Can you pls help me accomplish this in LINUX. Thanks... (5 Replies)
Discussion started by: dsfreddie
5 Replies

6. Emergency UNIX and Linux Support

Replacing dates]] with (dates)]]

Hi guys, For my wiki site I need to fix 1400 pages that use the wrong date format, most pages (not all) use eg. 1988]] I need to change that to (1988)]] The date range goes back to 1400 so I guess I need to do the following ssh into my server, dump mysql database vi .sql dump search... (20 Replies)
Discussion started by: lawstudent
20 Replies

7. Programming

SQL: find if a set od dates falls in another set of dates

Don't know if it is important: Debian Linux / MySQL 5.1 I have a table: media_id int(8) group_id int(8) type_id int(8) expiration date start date cust_id int(8) num_runs int(8) preferred_time int(8) edit_date timestamp ON UPDATE CURRENT_TIMESTAMP id... (0 Replies)
Discussion started by: vertical98
0 Replies

8. Shell Programming and Scripting

Dates not comparing correct even the same format

I have the date of the file passed into a variable also current date formatted same passed into a separate variable and compare the two with an if statement and statement always comes up false. Even though I verified the dates. Any help would be awesome. Filecrtdate=`ls -l $i | awk '{print... (19 Replies)
Discussion started by: coderanger
19 Replies

9. Shell Programming and Scripting

Please correct this

I have input file like this Input file: ABC|abc_etc_passwd XYZ|XYZ_etc_passwd zXY|XYZ_etc_passwd IJK|test_etc_passwd KLM|test_etc_passwd i want to do following in a loop. grep 'ABC' *abc_etc_passwd* grep 'XYZ' *XYZ_etc_passwd* grep 'ZXY' *ZXY_etc_passwd* i have tried this for i... (2 Replies)
Discussion started by: pinnacle
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