P
P
pashaxp2014-01-28 12:12:07
linux
pashaxp, 2014-01-28 12:12:07

Bash script, time comparison?

There is a bash script in which I make a selection from the mysql database, and get, for example, the time of the last change of some parameter
$ ./check.sh
2014-01-28 11:07:10
how can I compare the result with the current time ? For example, is the value received from the script more than 20 minutes ago compared to the current time on the server or not?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Goryachev, 2014-01-28
@pashaxp

You can compare the time directly in the query and display the difference or
display the current time (at the time the script was executed) and the time of the change.
Getting current time in mysql NOW() function

S
Sergey Sokolov, 2014-01-28
@sergiks

We translate the time into seconds since the beginning of the Unix epoch, and count it as integers. More or less like this:

SQLDATE="2014-01-28 11:07:10"
# или
SQLDATE=`./check.sh`

DATE_SQL=`date --date="$SQLDATE" +"%s"`
DATE_NOW=`date +"%s"`
DATE_DIFF_MINUTES=$[ ($DATE_NOW - $DATE_SQL) / 60 ]

if [ $DATE_DIFF_MINUTES -gt 20 ]; then
    # do stuff
fi

Make sure the timezones in SQL and the system are set the same. Or add the zone like this:date --date="2014-01-28 11:07:10 +04:00"

M
Maxim Tikhonenkov, 2014-01-28
@mtikhon

$ (date -d "2014-01-28 11:07:10" +%s ; date +%s) > 3
[email protected] [] /applications/dmsuat/environments/ucqa/node/delete
$ cat 3
1390907230
1390908465
[email protected] [] /applications/dmsuat/environments/ucqa/node/delete
$
The file contains two numbers in seconds, your time and the current time. They must be subtracted and you get what you need . You
can subtract expr $var1 - $var2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question