![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 11: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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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
|
|||
|
|||
|
Thanks - I have found a way myself now...
datestamp=`date '+%Y%m%d'` yest=$((datestamp -1)) Cheers!! |
|
#4
|
||||
|
||||
|
Quote:
Here is a script that can do date calculations without the use of external programs. |
|
#5
|
|||
|
|||
|
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
|
||||
|
||||
|
Quote:
|
|
#7
|
|||
|
|||
|
# ./datecalc -a 2003 11 12 - 1 | read y m d ; echo ${y}${m}${d}
y: Undefined variable any ideas? |
|||
| Google The UNIX and Linux Forums |