Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Dear All,
I'm using following script to calculate Yesterday's Date as a portion of a huge script.#!/bin/ksh
set -A DAYS Sat Sun Mon Tue Wed Thu Fri Sat
set -A MONTHS Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
YESTERDAY=$((`date +%d` -1))
MONTH=`date +%m`
YEAR=`date +%y`
NDAY=`date +%u`
WEEKDAY=${DAYS[`date +%u`]}
if [ $YESTERDAY -eq "0" ]
then
MONTH=$((MONTH-1))
if [ $MONTH -eq "0" ]
then
MONTH=12
YEAR=$((YEAR-1))
fiset `cal $MONTH $YEAR`
shift $(($# - 1))
YESTERDAY=$1
fi
TMONTH=${MONTHS[MONTH]}
$YEAR
echo $YESTERDAY$MONTH$YEARDate format that I need is yymmdd (030203). When d or m lessthan 10 required date format doesn't maintain.
e.g. if the date is 030203 it displays Yesterday's date as 03022 (which should be 030202).
Appreciate your help to resolve this Prb.

I think to do this properly you have to convert to a julian date format, do the arithmetic, and convert back. I had to write something similar recently and I started out trying to do it the easy way and it just got difficult. It's a bit long to post here but it's at http://www.williamrobertson.pwp.blueyonder.co.uk/code/shift_date.txt (or www.williamrobertson.net, Korn shell, shift_date).
btw, why declare #!/bin/ksh then use sh syntax with
if [ $MONTH -eq "0" ]when you can do it in ksh with
if [[ $MONTH = 0 ]]
or even (if MONTH is an integer though I don't see it declared as one)
if (( MONTH == 0 ))Also the usual convention is uppercase for exported variables, lowercase for others.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |