compare dates in shell script

Posted on 2009-02-13
Medium Priority
Last Modified: 2012-05-06
I need to compare 2 dates (date now and one from a file) and if date now is 20 minutes more than the date in the file, the script needs to restart the browser.

cat lastplaylist    gives me the date in the file:Fri Feb 13 05:48:51 GMT 2009
date gives me date and time now: Fri Feb 13 08:48:51 GMT 2009

I know I can restart the browser by
killall firefox
firefox http://mywebsite.com

I just don't know how to compare the 2 dates and see the difference in minutes.

Could someone help?

Many thanks
Question by:zeraxis_ee
LVL 23

Accepted Solution

Maciej S earned 2000 total points
ID: 23631058
1200 is 20 minutes in seconds.
DATE_NOW=`date +%s`
DATE_FROM_FILE=`date -d "\`cat lastplaylist\`" +%s`
if [ ${DATE_DIFF} -gt 1200 ]; then
   killall firefox
   firefox http://mywebsite.com/

Open in new window


Author Comment

ID: 23632668
That works perfectly... thank you

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question