Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
General
cancel
Showing results for 
Search instead for 
Did you mean: 

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
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!