Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.