Can any one guie me how to find yesterday's date (taking care of the leap years as well)in the format yymmdd using a shell script. 

Regards.

Recommended Answers

All 14 Replies

The gnu version of the "date" command (as used in linux/cygwin) has this functionality built in already.

If you are using linux you can simply use

date --date=yesterday +%y%m%d

See man date for more details.

#! /bin/bash
######## Created By - R.Wesley Dev Andrew #########

date=`date +%d-%m-%Y`
#echo "Please input date in format dd-mm-yyyy"
#read date

day=`echo $date | awk -F\- '{print $1}'`
month=`echo $date | awk -F\- '{print $2}'`
year=`echo $date | awk -F\- '{print $3}'`

export date day month year

if [ ${day}${month} -eq 0101 ]
then
        yesterdate=31-12-`expr $year - 1`
        echo $yesterdate
        exit
fi

if [ $day -eq 01 ]
then
        yestermonth=`expr $month - 1`
        yestermonth=`printf "%02d\n" $yestermonth`
        yesterday=`cal $yestermonth $year | tail -1 | head -1 | awk '{print $NF}'`
        if [ $yesterday ]
        then
                yesterday=`printf "%02d\n" $yesterday`
        else
                yesterday=`cal $yestermonth $year | tail -2 | head -1 | awk '{print $NF}'`
                if [ $yesterday ]
                then
                        yesterday=`printf "%02d\n" $yesterday`
                else
                        yesterday=`cal $yestermonth $year | tail -3 | head -1 | awk '{print $NF}'`
                        yesterday=`printf "%02d\n" $yesterday`
                fi
        fi
        yesterdate=${yesterday}-${yestermonth}-${year}
        echo $yesterdate
        exit
fi

yesterday=`expr $day - 1`
yesterday=`printf "%02d\n" $yesterday`
yesterdate=${yesterday}-${month}-${year}
echo $yesterdate
exit

If you are using ksh93 (Korn Shell 93) , date manipulation is builtin, i.e.

$ printf "%(%y%m%d)T\n" yesterday
100318
$

Hello,

always mindboggled to see what handy options or builtins are there...
Making it portable back to let's say Version 7 awk comes into mind:

<code>
awk 'BEGIN{ printf("%s\n", strftime("%Y%m%d", systime()-86400)) }'
</code>

Have fun,
issue9

Sorry

awk 'BEGIN{ printf("%s\n", strftime("%Y%m%d", systime()-86400)) }'
#! /bin/bash
######## Created By - R.Wesley Dev Andrew #########

date=`date +%d-%m-%Y`
#echo "Please input date in format dd-mm-yyyy"
#read date

day=`echo $date | awk -F\- '{print $1}'`
month=`echo $date | awk -F\- '{print $2}'`
year=`echo $date | awk -F\- '{print $3}'`


There no need for awk, and certainly not three calls to it.

Either of these will work in any Bourne-type shell:

date=`date +"%Y %m %d"`
read year month day <<.
$date
.
eval "`date "+date=%Y-%m-%d year=%Y month=%m day=%d"`"

And there are other ways it can be done using only the one external command, date.

export date day month year

if [ ${day}${month} -eq 0101 ]
then
        yesterdate=31-12-`expr $year - 1`


All standard Unix shells have integer arithmetic builtin; there's no need for an external command:

yesterdate=31-12-$(( $year - 1 ))

There is a chapter on date manipulation in my first book, Shell Scripting Recipes. That chapter is available online at http://cfajohnson.com/shell/ssr/08-The-Dating-Game.shtml.

commented: Great link +0

It was created compatible with bourne shell.
Can be modified for user input by swapping the commented lines.
Hence the clumsy work.

Might be improved for Bourne shell too. But I'm done with it now. If you come across a bug though, please let us know in the forum.

Nice suggestions though. And the dating link you have given is really cool.

Thanks & Regards,
Wesley Dev Andrew

Very simple guys.

SOLARIS_MACHINE$previousDate=`TZ="GMT+24" date +'%Y%m%d'`
SOLARIS_MACHINE$echo $previousDate
20101025


Regards,
ethi

ethi, This TZ is the best solution I have seen so far here. Its working for me. Thank you. However caution to any one if new to this,
1) Be sure to use the substitution to avoid changing TZ of the whole script.
2) Please ensure that you use the TZ increment appropriate for you location. For example, Indian Timezone is GMT+5:30. Hence the increment value = time calculation(24:00 - 05:30). The final command will be:

TZ="GMT+18:30" date +'%Y%m%d'

ethi, This TZ is the best solution I have seen so far here. Its working for me. Thank you. However caution to any one if new to this,
1) Be sure to use the substitution to avoid changing TZ of the whole script.
2) Please ensure that you use the TZ increment appropriate for you location. For example, Indian Timezone is GMT+5:30. Hence the increment value = time calculation(24:00 - 05:30). The final command will be:

TZ="GMT+18:30" date +'%Y%m%d'

Changing the value of TZ is unreliable (e.g., at the change to or from daylight savings time).

It was created with the Bourne shell. It can be changed for user input and comment lines of trade. Therefore, a clumsy work. Could be improved and the Bourne shell too. But I am done with it now. If you find a bug, however, tell us in the forum.

Why use the Bourne shell?

It is very unlikely that you will encounter a system without a POSIX shell, or at the very least a Korn shell (on which the POSIX standard is largely based).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.