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

  Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Use date command to find last month

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 08-10-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 25 July 2016, 7:26 PM EDT
Location: NM
Posts: 10,776
Thanks: 428
Thanked 950 Times in 882 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 Unix and Linux 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 Unix and Linux 08-10-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 25 July 2016, 7:26 PM EDT
Location: NM
Posts: 10,776
Thanks: 428
Thanked 950 Times in 882 Posts
lorcan -
Your solution is just fine - but not all versions of date support -d
Sponsored Links
    #5  
Old Unix and Linux 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. Linux
Sponsored Links
    #6  
Old Unix and Linux 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 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
date command - getting previous month ab_2010 UNIX for Dummies Questions & Answers 1 06-15-2011 10:38 AM
print previous month (current month minus 1) with Solaris date and ksh slashdotweenie UNIX for Dummies Questions & Answers 7 05-14-2010 08:11 AM
Date command to obtain the last month is not working correctly.. tigersk UNIX for Dummies Questions & Answers 9 03-11-2010 01: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 04:19 AM
find out month from a date rudoraj UNIX for Dummies Questions & Answers 5 07-03-2007 08:21 AM



All times are GMT -4. The time now is 12:01 PM.