简体   繁体   中英

Get newer MySQL Date in Bash-Script


I despair at a simple bash script.
So..
I've a MySQL Database running in two different databases there tables which should be synced with the same data - now I'm on a script which should do that for me.
In the tables theres a column "editeddate" which updates itself when sth edited in this row.
So the bash script is getting this.
ie
Table_A: 2015-01-09 18:08:54
Table_B: 2015-01-09 18:10:09
and now it won't say which of these strings is newer ..
Here's the code snipped:

table_a=$(mysql -u UID -pPW -D DB -e "SELECT editeddate FROM table_a WHERE id=1" | sed '1d')    #gets sql data without columnname i.e. 2015-01-09 18:08:54
table_a_date=$(date -d "$table_a")                                                              #converts sql data in bash date
table_b=$(mysql -u UID -pPW -D DB -e "SELECT editeddate FROM table_b WHERE id=1" | sed '1d')    #gets sql data without columnname i.e. 2015-01-09 18:10:09
table_b_date=$(date -d "$table_b")                                                              #converts sql data in bash date

if [[ $table_a > $table_b_date ]];                                                              #if table_a is newer than table_b
then
    echo "table_a is newer"
elif [[ $table_a < $table_b_date ]];                                                            #otherwise if table_b is newer than table_a
then
    echo "table_b is newer"
else                                                                                            #else if both are the same
    echo "Nothing to is newer"
fi

Thanks :)

If you'd bothered to see what your date -d call is spitting out, you'd see it's something like

Fri Jan  9 18:08:54 CST 2015

You're not comparing dates. You're comparing STRINGS , which means that "Wednesday" is LARGER than "Thursday", because W comes after T in the alphabet.

Yo u'd have been fine if you were just comparing the raw 2015-01-09 ... strings you're pulling out of the database, because that's a numeric string, in most-significant-first ordering.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM