Bash – Convert thesql count(*) to int in bash

bashMySQL

I'd like to execute this and check if count is 0, 1 or > 1 in bash.

mysql -e "select count(*) from mydb.mydb;"

The output is:

+----------+
| count(*) |
+----------+
|        0 |
+----------+

I don't know how to parse this. Also this will be used in a cron job, so I can't have any output.

Best Answer

You could suppress the tab column name by:

ROW_CNT=$(mysql --raw --batch -e 'select count(*) from mydb.mydb' -s)
echo $ROW_CNT

Also, the semicolon at end your SQL command is unnecessary

Related Question