Use date command to find last month | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Use date command to find last month

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-10-2007
Cbish68 Cbish68 is offline
Registered User
 
Join Date: Mar 2006
Last Activity: 12 December 2011, 11:59 AM EST
Location: Woodstock, GA
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Use date command to find last month

#!/usr/bin/ksh

Does anyone have a good way to set a variable to last month?

For example, today is 20070810. I would like to use the date command to set a variable to last months %m code, which is 07. If I pluck this months value (08) and user expr to do simple math on it, it returns 7 (not 07). If I use that to write my script, adding a zero in front of it, the script will break when a double digit month comes along.

I either need to get last months two digit %m code from the date command, or I need a way to force the expr command to use two digits.

Any ideas?
Sponsored Links
    #2  
Old 08-10-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 25 November 2014, 10:33 PM EST
Location: NM
Posts: 10,282
Thanks: 288
Thanked 815 Times in 757 Posts
This will not work for a variety of reasons.
What happens on March 31 - You'll go back to February 31?
What happens on Jan 10?

Check the FAQ for perderabo's datecalc script. It will do just what you want.
Sponsored Links
    #3  
Old 08-10-2007
lorcan lorcan is offline
Registered User
 
Join Date: May 2007
Last Activity: 13 August 2012, 12:14 AM EDT
Posts: 224
Thanks: 0
Thanked 2 Times in 2 Posts

Code:
echo $(date -d"1 month ago" "+%G%m%d" | cut -c5-6)

or


Code:
echo $(date -d"1 month ago" "+%m" )

    #4  
Old 08-10-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 25 November 2014, 10:33 PM EST
Location: NM
Posts: 10,282
Thanks: 288
Thanked 815 Times in 757 Posts
lorcan -
Your solution is just fine - but not all versions of date support -d
Sponsored Links
    #5  
Old 08-10-2007
lorcan lorcan is offline
Registered User
 
Join Date: May 2007
Last Activity: 13 August 2012, 12:14 AM EDT
Posts: 224
Thanks: 0
Thanked 2 Times in 2 Posts
Yes thats true Jim. I should have posted that as a note.
Sponsored Links
    #6  
Old 08-10-2007
Cbish68 Cbish68 is offline
Registered User
 
Join Date: Mar 2006
Last Activity: 12 December 2011, 11:59 AM EST
Location: Woodstock, GA
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by jim mcnamara View Post
Check the FAQ for perderabo's datecalc script. It will do just what you want.
Yeah. That's some script. I feel like a total noob after reading that.

I appreciate the input. I think I am going to approach this from another angle, though.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
date command - getting previous month ab_2010 UNIX for Dummies Questions & Answers 1 06-15-2011 11:38 AM
print previous month (current month minus 1) with Solaris date and ksh slashdotweenie UNIX for Dummies Questions & Answers 7 05-14-2010 09:11 AM
Date command to obtain the last month is not working correctly.. tigersk UNIX for Dummies Questions & Answers 9 03-11-2010 02:45 AM
How to find the create time of a file if current date is in next month priyankak Shell Programming and Scripting 1 05-18-2009 05:19 AM
find out month from a date rudoraj UNIX for Dummies Questions & Answers 5 07-03-2007 09:21 AM



All times are GMT -4. The time now is 11:03 AM.