The UNIX and Linux Forums  

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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Korn Shell script not running Asty Shell Programming and Scripting 1 08-11-2006 10:17 AM
Help with Korn Shell script heprox AIX 1 12-19-2005 10:04 AM
korn shell script pavan_test UNIX Desktop for Dummies Questions & Answers 3 10-27-2005 09:09 AM
korn shell script pavan_test UNIX for Dummies Questions & Answers 1 10-26-2005 10:17 AM
Yesterdays Date/Date Arithmetic Perderabo Answers to Frequently Asked Questions 0 04-26-2004 02:04 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-12-2003
Registered User
 

Join Date: Aug 2003
Posts: 252
Korn Shell Script - Getting yesterdays date

I need to get yesterdays date in the format yyyymmdd
I can get today's date simply enough - 20031112

Is there any way to substract 1 from this easily enough in korn shell script?

It has to be korn shell and not perl
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 11-12-2003
jsilva's Avatar
Registered User
 

Join Date: Apr 2003
Posts: 169
Hi,

You can use the script below, it will do more than you asked, but you can easly modify it... it was written by Tapani Tarvainen.

#! /usr/bin/ksh
# Get yesterday's date in YYYY-MM-DD format.
# With argument N in range 1..28 gets date N days before.
# Tapani Tarvainen January 2002
# This code is in the public domain.

OFFSET=${1:-1}

case $OFFSET in
*[!0-9]* | ???* | 3? | 29) print -u2 "Invalid input" ; exit 1;;
esac

eval `date "+day=%d; month=%m; year=%Y`
typeset -Z2 day month
typeset -Z4 year

# Subtract offset from day, if it goes below one use 'cal'
# to determine the number of days in the previous month.
day=$((day - OFFSET))
if (( day <= 0 )) ;then
month=$((month - 1))
if (( month == 0 )) ;then
year=$((year - 1))
month=12
fi
set -A days `cal $month $year`
xday=${days[$(( ${#days[*]}-1 ))]}
day=$((xday + day))
fi

print $year-$month-$day
print $month/$day/${year#??}
Reply With Quote
  #3 (permalink)  
Old 11-12-2003
Registered User
 

Join Date: Aug 2003
Posts: 252
Thanks - I have found a way myself now...


datestamp=`date '+%Y%m%d'`
yest=$((datestamp -1))



Cheers!!
Reply With Quote
  #4 (permalink)  
Old 11-12-2003
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,318
Quote:
Originally posted by frustrated1
Thanks - I have found a way myself now...


datestamp=`date '+%Y%m%d'`
yest=$((datestamp -1))
That won't work on the first of the month.

Here is a script that can do date calculations without the use of external programs.
Reply With Quote
  #5 (permalink)  
Old 11-12-2003
Registered User
 

Join Date: Aug 2003
Posts: 252
Hi- Thanks - to be honest I hadnt thought of the first of the month

I am using your script as follows:
./datecalc -a 2003 11 12 - 1

and it returns 2003 11 11

Is there a way that it can return the value without the spaces?
Reply With Quote
  #6 (permalink)  
Old 11-12-2003
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,318
Quote:
Originally posted by frustrated1

I am using your script as follows:
./datecalc -a 2003 11 12 - 1

and it returns 2003 11 11

Is there a way that it can return the value without the spaces?
./datecalc -a 2003 11 12 - 1 | read y m d ; echo ${y}${m}${d}
Reply With Quote
  #7 (permalink)  
Old 11-12-2003
Registered User
 

Join Date: Aug 2003
Posts: 252
# ./datecalc -a 2003 11 12 - 1 | read y m d ; echo ${y}${m}${d}
y: Undefined variable


any ideas?
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:56 PM.


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