Operating System - HP-UX
1751807 Members
4237 Online
108781 Solutions
New Discussion юеВ

date manipulation in shell script

 
SOLVED
Go to solution
Paolo Gilli
Frequent Advisor

date manipulation in shell script

Hi,
I, i need to write a shell script where is possible assign to a variable the 'date' value of +n or -n days starting from current date
In other words:
I have:
SYS_DATE="$( date '+%Y%m%d' )"

how can I do something like:
YESTERDAY_DATE= "$( date '+%Y%m%d' )" -1
or
TOMORROW_DATE= "$( date '+%Y%m%d' )" +1

avoid writing date calculation routines?

any ideas?
Tahnkyou
6 REPLIES 6
Patrick Wallek
Honored Contributor
Solution

Re: date manipulation in shell script

Use a date calc routine that already written. Search for A. Clay Stephensons caljd.sh or caljd.pl script. It should be easy to find in the forums. His script will do exactly what you want.
Patrick Wallek
Honored Contributor

Re: date manipulation in shell script

You can download caljd.sh or caljd.pl from here:

http://www.cmve.net/~merijn/#Contrib

It is at the bottom of the page.
A. Clay Stephenson
Acclaimed Contributor

Re: date manipulation in shell script

I'll make it easy for you and this is the latest version of caljd.sh.

YESTERDAY_DATE=$(caljd.sh -s -y $(caljd.sh -p 1))
echo "Yesterday = ${YESTERDAY_DATE}"
TOMORROW_DATE=$(caljd.sh -s -y $(caljd.sh -n 1))
echo "Tomorrow = ${TOMORROW_DATE}"

Invoke as caljd.sh -u for full usage and many examples (like previous date plus skip over weekends).

If it ain't broke, I can fix that.
Arturo Galbiati
Esteemed Contributor

Re: date manipulation in shell script

I'm using the mktime SW and it's great!
http://members.rogers.com/weirdways/technogeek/source/mktime/

Art
Paolo Gilli
Frequent Advisor

Re: date manipulation in shell script

SUPER!!!
Thankyou
Regards
Paolo
Paolo Gilli
Frequent Advisor

Re: date manipulation in shell script

An other great help by Art!
Thakyou!