Answer the question
In order to leave comments, you need to log in
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
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
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
date --date="2014-01-28 11:07:10 +04:00"
$ (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 questionAsk a Question
731 491 924 answers to any question