The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
back to back printing in UNIX amirthraj_12 AIX 3 05-06-2008 04:42 AM
Back-to-Back Connection using HBAs aldowsary IP Networking 2 11-12-2007 03:43 PM
sed command for using with back slashes seaten UNIX for Dummies Questions & Answers 2 07-25-2005 08:59 AM
Calendar date to Julian and Back BCarlson Shell Programming and Scripting 4 05-14-2005 02:18 PM
Back Command Jatic UNIX for Dummies Questions & Answers 3 04-25-2004 10:20 AM

Closed Thread
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 09-28-2001
Registered User
 

Join Date: Mar 2001
Location: UK
Posts: 20
Need to go back 1 day using the date command

I am trying to write a shell script to look at log files with dates in the file name.

Now I know how to use the expr command to subtract 1 day from the other, which is simple when the dates are from the 2nd to the 31st of each month.

But the problem I have is when the date turns to the 1st and the last month is 31st or 30th - because 1 -1 = -1 don't it, rather than 30 or 31

How do I write a script to handle this.

P.S I'm using KSH
Forum Sponsor
  #2  
Old 09-28-2001
LivinFree's Avatar
Goober Extraordinaire
 

Join Date: Jul 2001
Location: Portland, OR, USA
Posts: 1,584
Please search the forums, this has been brought up a few times today alone... Here are a few I found in a minute of searching:

Date Substraction

Date function question < -- That one has a few different ways of doing it...
  #3  
Old 09-28-2001
Registered User
 

Join Date: Dec 2000
Location: Greater Dallas area
Posts: 405
This script was posted here some time ago, and I modified it slightly.
Code:
#!/usr/bin/ksh
date '+%m %d %Y' | 
{ 
read MONTH DAY YEAR
DAY=`expr "$DAY" - 1` 
case "$DAY" in 
        0) 
           MONTH=`expr "$MONTH" - 1` 
                case "$MONTH" in 
                        0) 
                           MONTH=12 
                           YEAR=`expr "$YEAR" - 1` 
                        ;; 
                esac 
        DAY=`cal $MONTH $YEAR | grep . | fmt -1 | tail -1` 
esac 
echo "Yesterday was: $MONTH $DAY $YEAR" 
}
__________________
[url=http://chuckb.1le.net/]My website[/url]
  #4  
Old 12-06-2001
Cameron's Avatar
Registered User
 

Join Date: Nov 2001
Location: Brisbane, Australia
Posts: 490
Ok, I understand the script and provided the following result ...
Yeseterday was: 6 12 2001 .

But how does one convert the month (12) into either the long or short name(ie - December/Dec).

I need to do this as Directories are named ... (eg - Nov2001, Dec2001, etc.). I'm trying to create a reporting script to run via cron on the 1st of every month perusing the pervious months sar files for system reports. The scripts purpose is to be run from cron and the command line if required (for reruns for eg.).

Any help appreciated - have already done a search.
Such is the life of a newbie.

Last edited by Cameron; 12-07-2001 at 06:37 PM.
  #5  
Old 12-11-2001
Cameron's Avatar
Registered User
 

Join Date: Nov 2001
Location: Brisbane, Australia
Posts: 490
Thumbs up Solved ...

Try the following ...

## Long Name Month Listing.
lngmth="January February March April May June \
July August September October November December"
## Short Name Month Listing.
shtmth="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"

##
## Get Current Month Number (01-12).
xcurmth=`date +%m`
## Get Previous Month Number (01-11).
xprvmth=`expr $xcurmth - 1`
## Get Year Number (??).
xcuryr=`date +%Y`
## Get Previous Year Number (??).
xprvyr=`expr $xcuryr - 1`

xx=`echo $shtmth | cut -f$2 -d" "`
zz=`echo $lngmth | cut -f$2 -d" "`
XX=`echo $shtmth | cut -f$2 -d" "`$xprvyr
ZZ=`echo $lngmth | cut -f$2 -d" "`$xcuryr


If $xprvmth = 0 then you must use $xprvyr and 12 for the month. ...
(eg. `echo $shtmth | cut -f12 -d" "`$xprvyr)
Google The UNIX and Linux Forums
Closed Thread

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:51 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0