The UNIX and Linux Forums

The UNIX and Linux Forums (http://www.unix.com/index.php)
-   Shell Programming and Scripting (http://www.unix.com/shell-programming-and-scripting/)
-   -   Korn Shell Script - Getting yesterdays date (http://www.unix.com/shell-programming-and-scripting/11953-korn-shell-script-getting-yesterdays-date.html)

frustrated1 11-12-2003 10:10 AM

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

jsilva 11-12-2003 11:00 AM

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#??}

frustrated1 11-12-2003 11:33 AM

Thanks - I have found a way myself now...


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



Cheers!!

Perderabo 11-12-2003 11:59 AM

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.

frustrated1 11-12-2003 01:33 PM

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?

Perderabo 11-12-2003 02:40 PM

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}

frustrated1 11-12-2003 02:47 PM

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


any ideas?


All times are GMT -4. The time now is 04:19 PM.

Linux and Unix Supported by: vBulletin
Search Engine Optimisation provided by DragonByte SEO v1.1.4 (Pro) - vBulletin Mods & Addons Copyright © 2014 DragonByte Technologies Ltd.
The UNIX and Linux Forums Content Copyright ©1993-2013. All Rights Reserved.
Forum Operations by The UNIX and Linux Forums