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
Compare date from db2 table to yesterday's Unix system date sasaliasim Shell Programming and Scripting 8 04-24-2008 03:04 AM
Perl: Extracting date from file name and comparing with current date MKNENI Shell Programming and Scripting 4 03-26-2008 12:01 PM
date issue-find prevoius date in a patricular format bsandeep_80 UNIX for Advanced & Expert Users 3 11-15-2007 04:42 PM
Changing Creation Date to a Prespecified Date of a File In Unix monkfan UNIX for Dummies Questions & Answers 4 11-28-2006 03:15 AM
Substraction in bash mclaudiu SUN Solaris 2 11-23-2005 06:00 AM

Closed Thread
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-28-2001
AC AC is offline
Registered User
 

Join Date: Mar 2001
Posts: 2
Question

In Unix script, how to get a "date - 1" ie, yesterday?
Forum Sponsor
  #2 (permalink)  
Old 03-28-2001
mib mib is offline
Registered User
 

Join Date: Jan 2001
Location: Calicut
Posts: 228
#!/bin/bash

# figure out what yesterday was.
date '+%Y %m %d' |
{
read year month day

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 "Yeseterday was: $day $month $year"
}


  #3 (permalink)  
Old 03-28-2001
PxT's Avatar
PxT PxT is offline
Registered User
 

Join Date: Oct 2000
Location: Sacramento, CA
Posts: 905
or if you are using GNU date, simply do:

date --date=yesterday
  #4 (permalink)  
Old 03-28-2001
AC AC is offline
Registered User
 

Join Date: Mar 2001
Posts: 2
Thumbs up Date Substraction

It works. Thank you.
  #5 (permalink)  
Old 03-29-2001
mib mib is offline
Registered User
 

Join Date: Jan 2001
Location: Calicut
Posts: 228
in Perl there is really handy way to manipulate Date

print scalar localtime (time() - 86400 * n);
it will substract n from current date and print the date

print scalar localtime (time() - 86400 * 1); #yesterday
print scalar localtime (time() - 86400 * 30); #Date of 30 days back

To get date four days from now
print scalar localtime (time() + 86400 * 4);

and so on....
Google UNIX.COM
Closed Thread

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:31 AM.


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

Content Relevant URLs by vBSEO 3.2.0