我有一个简单的bash脚本。我希望得到数据库每个表中行数的精确计数。
#!/bin/bash
TABLES_OLD=$( mysql -u user -ppassword MySchema --batch --skip-column-names -e"SHOW TABLES FROM MySchema" )
for table in "${TABLES_OLD[@]}"
do
QUERY="SELECT COUNT(*) FROM ${table}"
echo "${QUERY}"
done
脚本打印:
SELECT COUNT(*) FROM Table 1
Table2
Table3
Table4
etc...
显然,这不是我想要的,我甚至不知道发生了什么是可能的。我究竟做错了什么?
答案 0 :(得分:1)
Try this, put the tables into an array then loop thru the results
db_host='host'
db_user='user'
db_pass='password'
db='your_db'
read -ra var_id <<< $(mysql -h $db_host -u $db_user -p$db_pass $db -sse "show tables from $db")
for i in "${var_id[@]}";
do
results=$(mysql -h $db_host -u $db_user -p$db_pass $db -sse "select count(*)from $i")
echo "$i $results"
done
答案 1 :(得分:0)
这应该这样做:
#/bin/bash
mysql -u user-ppassword -e "SELECT table_name, table_rows
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'your_data_base_name';"
答案 2 :(得分:-1)
将echo
替换为eval
代码将是
#!/bin/bash
TABLES_OLD=$( mysql -u user -ppassword MySchema --batch --skip-column-names -e"SHOW TABLES FROM MySchema" )
for table in "${TABLES_OLD[@]}"
do
QUERY="SELECT COUNT(*) FROM ${table}"
eval "${QUERY}"
done