Operating System - HP-UX
1824918 Members
3914 Online
109677 Solutions
New Discussion юеВ

Date computation in a shell script

 
SOLVED
Go to solution
Bala_4
Occasional Advisor

Date computation in a shell script

Hi All

Is there a way to add/subtract days using date function in a shell script ?
Please don't tell me that I have to write lengthy unix script .

for e.g I want to add 2 days to the current date in a shell script !!!
i.e expr "`date + 2`" or something like that

Thanks in advance

Bala

5 REPLIES 5
someone_4
Honored Contributor
Solution

Re: Date computation in a shell script

This has been asked before here is one link i found that should help you out.

http://forums.itrc.hp.com/cm/QuestionAnswer/1,1150,0x5fc3854994d9d4118fef0090279cd0f9,00.html

A. Clay Stephenson
Acclaimed Contributor

Re: Date computation in a shell script

Hi:

I have a simple utility that makes this duck soup. The script is complicated but it's already written - so who cares.

The idea is that we convert today's date to a true Jukian Day (~ num of days since 4004 BCE) and add 2 to it and then send that number back in to the script.

TODAY=`caljd.sh`
NEXTDATE=$((${TODAY} + 2))
NEWDATE=`caljd.sh ${NEXTDATE}`
echo ${NEWDATE}.

The attached script does all the hard work and if you execute caljd.sh -u it will display the full usage. The neat thing about this method is that it will work across months, leap years,
past or future, ...

Regards, Clay
If it ain't broke, I can fix that.
Jeffrey S. Sims
Trusted Contributor

Re: Date computation in a shell script

No long UNIX script just a simple line of perl

perl -e 'print scalar localtime (time +2 * 86400),"\n"'

will add 2 days to the current date

perl -e 'print scalar localtime (time -2 * 86400),"\n"'

will subtract 2 days from the current date

Hope this helps
Sridhar Bhaskarla
Honored Contributor

Re: Date computation in a shell script

Bala,

Unfortunately there is no single command to do arthmetic on dates. There are quite a number ways to do this. As perl is almost there on unix systems, you can use the method suggested by David in the thread

http://forums.itrc.hp.com/cm/QuestionAnswer/1,1150,0x2fc2854994d9d4118fef0090279cd0f9,00.html

or you can use the logic behind it.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Bala_4
Occasional Advisor

Re: Date computation in a shell script

Thanks Everyone for the quick replies...

Thanks again

Bala