Date calculation

I have to run a shell , only if file_a.txt is updated correctly before 1hr.

that is,

file_a.txt
2011_09_21__14:10:20  --> any format..

And

the shell can be like,
if [ (date now) - (date from file_a.txt) > 1hr ] ; then
Run some shell
update file_a.txt with (date now)
fi

how to do this ?

Some clarifying questions:

  • Does file_a.txt contain the timestamp, or do you need to use the filesystem timestamp?
  • If it contains it: should file_a.txt contain only 1, or more timestamps?
  • What OS are you using?

i am sorry , if you are not clear from my description.

file_a.txt contains only one line , which is updating by us. [ie; time]

It can be in any format , i prefer something like:
201109211410

OS : fedora / CentOS

---------- Post updated at 05:49 PM ---------- Previous update was at 04:33 PM ----------

I got so many scripts to get / change date format..

But not the same what i asked , hitting on my head :wall:

The format you're proposing would work. Just use man date (Linux) to output the date/time appropriately, and then substract from it the current time (same format), so you'd get (for example)

diff = $((201109211410 - 201109211310 ))
if [[ $diff -ge 100 ]]
then
  echo "1 hour has passed"
else
  echo "Wait a bit longer"
fi

What do you mean by "any format" because if the format changes then any script no matter how well written wont work so decide on a format and stick to it and use perl...but if you insist on the shell then a format like YYYYMMDHHMMSS is viable for time diffs.

pludi ,

But i have a doubt in this,
think about this condition,

befo_=110921232049 --> 21/09/2011 23:20:49
now_=110921233957 --> 21/09/2011 23:39:57

now_ - befo_ = 1908 [Which is > 100 !!! But just 19min only passed]

Again if we think about 1000 [ie; with Hour-Digit or 10000... ] same problem will be there.

.

---------- Post updated at 11:51 PM ---------- Previous update was at 11:47 PM ----------

Anyway,

i changed the entire logic. I am not using these things..

Made this function alone in my shell with another parameter... :rolleyes: :wink: :smiley:

Thanks.

Well, then stay consistent. In your original example, you gave the date as YYYYMMDDhhmm, so anything >= 100 would mean 1 hour has passed. Now you're using YYMMDDhhmmss, you if you apply even a tiny bit of logical thinking you'd see that 100 wouldn't fit anymore, but that you have to shift it left by 2 places!!