Korn Shell Script - Getting yesterdays date | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Korn Shell Script - Getting yesterdays date

Shell Programming and Scripting


Tags
shell script, shell scripting, unix scripting, unix scripting basics

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-12-2003
frustrated1 frustrated1 is offline
Registered User
 
Join Date: Aug 2003
Last Activity: 5 September 2014, 6:44 AM EDT
Location: Ireland
Posts: 296
Thanks: 2
Thanked 1 Time in 1 Post
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
Sponsored Links
    #2  
Old 11-12-2003
jsilva's Avatar
jsilva jsilva is offline
Registered User
 
Join Date: Apr 2003
Last Activity: 28 July 2010, 3:08 PM EDT
Posts: 169
Thanks: 0
Thanked 2 Times in 1 Post
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#??}
Sponsored Links
    #3  
Old 11-12-2003
frustrated1 frustrated1 is offline
Registered User
 
Join Date: Aug 2003
Last Activity: 5 September 2014, 6:44 AM EDT
Location: Ireland
Posts: 296
Thanks: 2
Thanked 1 Time in 1 Post
Thanks - I have found a way myself now...


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



Cheers!!
The Following User Says Thank You to frustrated1 For This Useful Post:
blacksteel1988 (05-21-2012)
    #4  
Old 11-12-2003
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 20 November 2014, 5:33 PM EST
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 64
Thanked 438 Times in 261 Posts
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.
Sponsored Links
    #5  
Old 11-12-2003
frustrated1 frustrated1 is offline
Registered User
 
Join Date: Aug 2003
Last Activity: 5 September 2014, 6:44 AM EDT
Location: Ireland
Posts: 296
Thanks: 2
Thanked 1 Time in 1 Post
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?
Sponsored Links
    #6  
Old 11-12-2003
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 20 November 2014, 5:33 PM EST
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 64
Thanked 438 Times in 261 Posts
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}
Sponsored Links
    #7  
Old 11-12-2003
frustrated1 frustrated1 is offline
Registered User
 
Join Date: Aug 2003
Last Activity: 5 September 2014, 6:44 AM EDT
Location: Ireland
Posts: 296
Thanks: 2
Thanked 1 Time in 1 Post
# ./datecalc -a 2003 11 12 - 1 | read y m d ; echo ${y}${m}${d}
y: Undefined variable


any ideas?
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Korn Shell Date Formatting ther2000 Shell Programming and Scripting 2 07-01-2011 11:40 AM
how to get the yesterdays date? jam_prasanna Shell Programming and Scripting 2 07-19-2010 04:29 AM
How to find yesterdays file - shell script DallasT Shell Programming and Scripting 6 06-13-2010 07:08 AM
Filtering the yesterdays date from log files via script. linuxgeek Shell Programming and Scripting 2 01-21-2010 07:00 AM
yesterdays date sujju1985 Shell Programming and Scripting 2 12-29-2008 06:49 PM



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