The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help, I need to get the last date of previous month sirrtuan Shell Programming and Scripting 11 10-14-2008 05:59 AM
last month end date vanathi UNIX for Advanced & Expert Users 7 03-21-2008 04:17 PM
find out month from a date rudoraj UNIX for Dummies Questions & Answers 5 07-03-2007 08:21 AM
Command to find out user creation date Crazy_murli UNIX for Dummies Questions & Answers 6 01-08-2007 09:30 AM
how to get month last date in unix rajan_ka1 Shell Programming and Scripting 12 10-04-2005 07:20 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-10-2007
Cbish68 Cbish68 is offline
Registered User
  
 

Join Date: Mar 2006
Location: Woodstock, GA
Posts: 17
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?
  #2 (permalink)  
Old 08-10-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
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.
  #3 (permalink)  
Old 08-10-2007
lorcan lorcan is offline
Registered User
  
 

Join Date: May 2007
Posts: 219
Code:
echo $(date -d"1 month ago" "+%G%m%d" | cut -c5-6)
or

Code:
echo $(date -d"1 month ago" "+%m" )
  #4 (permalink)  
Old 08-10-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
lorcan -
Your solution is just fine - but not all versions of date support -d
  #5 (permalink)  
Old 08-10-2007
lorcan lorcan is offline
Registered User
  
 

Join Date: May 2007
Posts: 219
Yes thats true Jim. I should have posted that as a note.
  #6 (permalink)  
Old 08-10-2007
Cbish68 Cbish68 is offline
Registered User
  
 

Join Date: Mar 2006
Location: Woodstock, GA
Posts: 17
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

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




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


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0