assign yesterday's date to variable

Hi,

i am trying to assign yesterday's date to a variable on below system -

Machine hardware: sun4u
OS version: 5.9
Processor type: sparc

usr> setenv dt `date +"%Y%m%d"`
usr> echo $dt
20100820

i am able to assign today's date but similarly i want to assign yesterday's date.

Plz help

Thankx!!

Try this:

yest.sh

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 "$MONTH$DAY$YEAR"  
}

other script:

yest=`yest.sh`
echo $yest
# sh somescript.sh
08192010

Thankx for the reply but i am looking for result in YYYYMMDD format

and you cant figure that out?

Look here!

....
echo "$MONTH$DAY$YEAR" 

got it!! thankx!!

try:

info date

maybe:

date -d yesterday
date --date=yesterday
date -d '1 day ago'

Default date in Solaris don't support these options, such as: show yesterday, show several days/times ago, etc.

You need install GNU date in Solaris. It will make easy for your life.

the GNU date is in coreutils package, which you can download from sunfreeware.com.au and install directly.

1 Like

If you installed coreutils package, you could do:

setenv dt `date -d yesterday +"%Y%m%d"`
echo $dt

hello guys...at first I'm sorry to post this question here as I cannot find a way to post questions on this website.

Question:

I want to pull the logs for Today's date or say last 24 hours.

for ex: grep logs message | grep Jan 5 -- using this syntax I can pull all the logs for Jan 5 however I want to use this in a script hence need to know a way to achieve this.

Please help. Thanx