Link to home
Start Free TrialLog in
Avatar of mbergeby
mbergeby

asked on

get yesterdays date into a unix variable

Hi,
I'm working in HP-ux 11 and I'm trying to get yesterdays date into a variable that I can use later on in a script.
I use the format:
DATE=`date '+%d-%b-%Y'`
which gives me i.e. 01-Jan-2007
I make it uppercase and gets it into a variable with
export Y_DATE=$(printf "%s\n" "$DATE" | tr '[a-z]' '[A-Z]')
which gives me 01-JAN-2007 as value in Y_DATE
The question is how can I get the result to be 31-DEC-2006 in Y_DATE??

Thankfull for any help
Avatar of vikaskhoria
vikaskhoria
Flag of India image

Hi Check out the following code:

#!/usr/bin/ksh
date '+%m %d %Y' |
{
read MONTH DAY YEAR
DAY=`expr "$DAY" - 1`
case "$DAY" in
        0)
           MONTH=`expr "$MONTH" - 1`
                case "$MONTH" in
                        0)
                           MONTH=12
                           YEAR=`expr "$YEAR" - 1`
                        ;;
                esac
        DAY=`cal $MONTH $YEAR | grep . | fmt -1 | tail -1`
esac
echo "Yesterday was: $MONTH $DAY $YEAR"
}
You can use this logic in your code!!
Avatar of nixfreak
nixfreak

Y_DATE=`TZ=CST+24 date '+%d-%b-%Y'`
ASKER CERTIFIED SOLUTION
Avatar of nixfreak
nixfreak

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mbergeby

ASKER

Thanks both!
nixfreak, your solution was very simple and effective and with the correct formating which was important for me, thanks again :-)