Korn Shell Script - Getting yesterdays date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Korn Shell Script - Getting yesterdays date
# 1  
Old 11-12-2003
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
# 2  
Old 11-12-2003
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#??}
# 3  
Old 11-12-2003
Thanks - I have found a way myself now...


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



Cheers!!
This User Gave Thanks to frustrated1 For This Post:
# 4  
Old 11-12-2003
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.
# 5  
Old 11-12-2003
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?
# 6  
Old 11-12-2003
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}
# 7  
Old 11-12-2003
# ./datecalc -a 2003 11 12 - 1 | read y m d ; echo ${y}${m}${d}
y: Undefined variable


any ideas?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to do Date Manupulation in Korn Shell?

Hi All, I need to find the date 19days back from the current date: eg: if today is 17 March 2013 then the output should be : 26 Feb 2013 Can i do this using date command in Korn Shell? And also if i need future 15 days date from current date, how to that? Any help appreciated :) ... (3 Replies)
Discussion started by: Arun Mishra
3 Replies

2. Shell Programming and Scripting

Get yesterdays Date for Input Date

Hi, I have been trying to get the yesterdays date for the Input date I pass. I know how to do for the current timestamp but how to do for the input date. Is there any way I can convert to epoch time and do manipulations and back to human readable date? Please help Thanks ... (1 Reply)
Discussion started by: abhi1988sri
1 Replies

3. Shell Programming and Scripting

Korn Shell Date Formatting

I am serching for a file created today like so: TODAY=$(date +"%b-%d") T_FILE=$(find /export/home/dan/ck/reports/t-status-t.txt-$TODAY-05-00-0?.csv) The file it is searching for is titled: /export/home/dan/ck/reports/t-status-t.txt-Jun-29-05-00-01 however, I when... (2 Replies)
Discussion started by: ther2000
2 Replies

4. Shell Programming and Scripting

how to get the yesterdays date?

Hi All, Can anybody help me to get the yesterdays date in perl script. My script is as below #!/bin/perl -w $yes=system("TZ=IST+24 date +%d-%m-%Y"); print "$yes\n"; script is writting the date but with 0 pls see the output below #!/bin/perl -w $yes=system("TZ=IST+24 date... (2 Replies)
Discussion started by: jam_prasanna
2 Replies

5. Shell Programming and Scripting

How to find yesterdays file - shell script

Hey guys - i have a script (below) that searches for current files in a particular directory. However i was wondering how to make it search for "yesterdays" file. For instance it looks for a file from yesterday and no older than that. I used stat command to check for file information: ... (6 Replies)
Discussion started by: DallasT
6 Replies

6. Shell Programming and Scripting

Filtering the yesterdays date from log files via script.

hi All, I have this sample text file - access.log: Jan 18 21:34:29 root 209.151.232.70 Jan 18 21:34:40 root 209.151.232.70 Jan 18 21:34:43 root 209.151.232.70 Jan 18 21:34:56 root 209.151.232.70 Jan 18 21:35:10 root 209.151.232.70 Jan 18 21:35:23 root 209.151.232.70 Jan 18 21:36:04 root... (2 Replies)
Discussion started by: linuxgeek
2 Replies

7. UNIX for Dummies Questions & Answers

Need to pull Yesterdays Date...

I tried this and it works for the most part, but if the date is 20090301, it displays 20090300. YESTERDAY=$((`date +%Y%m%d` -1)) (2 Replies)
Discussion started by: cards0622
2 Replies

8. UNIX for Dummies Questions & Answers

How to get yesterdays julian date

Hi, Was using date +%Y%j to get current julian date. Can anyone let me know how can I get y'day's julin date. Thx Did check FAQ but couldn't find anything. Thanks. (3 Replies)
Discussion started by: er_ashu
3 Replies

9. Shell Programming and Scripting

yesterdays date

To get yesterays date, execute the command : TZ=aaa24 date +%Y%m%d Output format will be yyyymmdd (2 Replies)
Discussion started by: sujju1985
2 Replies

10. UNIX for Dummies Questions & Answers

Get yesterdays date given todays date

Hi Guys. I am very new to UNIX. I need to get yesterdays and tommorows date given todays date. Which command and syntax do i use in basic UNIX shell. Thanks. (2 Replies)
Discussion started by: magikminox
2 Replies
Login or Register to Ask a Question