在 bash 中从 mysql 获取 count val 的最简单方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3886478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:45:14  来源:igfitidea点击:

Easiest way to get count val from mysql in bash

mysqlbash

提问by ghostdog74

Maybe i should use python or perl but i dont know any.

也许我应该使用 python 或 perl,但我不知道。

I have 4 statements and i would like to check if there are any errors longer then an hour. My user is setup so i dont need to enter a mysql user/pass. This statement is in mysql_webapp_error_check.sh

我有 4 条语句,我想检查是否有任何错误超过一个小时。我的用户已设置,所以我不需要输入 mysql 用户/密码。该语句在 mysql_webapp_error_check.sh 中

#!/bin/bash
mysql testdb -e "select count(*) from tbl where last_error_date < DATE_SUB(NOW(), INTERVAL 1  HOUR);"

How do i make it give me the return value (count(*)) instead of printing to screen?

我如何让它给我返回值(计数(*))而不是打印到屏幕?

Then i'll write an if statement and output to stdout/err for cron to use to email me (otherwise i want the script to be silent so nothing is emailed unless theres a problem)

然后我会写一个 if 语句并输出到 stdout/err 以便 cron 用来给我发电子邮件(否则我希望脚本保持静音,所以除非出现问题,否则不会通过电子邮件发送任何内容)

采纳答案by DigitalRoss

#!/bin/bash
echo show databases\; | mysql -u root | (while read x; do
  echo "$x"
  y="$x"
done
echo "$y"
)

回答by MarcoHager

Searched the same, -s for silent works exactly for me.

搜索相同的, -s 对我来说完全适用。

#!/bin/bash
result=`mysql testdb -s -e "select count(*) from tbl where last_error_date < DATE_SUB(NOW(), INTERVAL 1  HOUR);"`
echo result = .$result.

PS.: There is also a --batch parameter in my mysql Ver 14.14 Distrib 5.1.49 which "Write fields without conversion. Used with --batch" so its a little off-topic here, but should be mentioned here.

PS.:在我的 mysql Ver 14.14 Distrib 5.1.49 中也有一个 --batch 参数,它“无需转换即可写入字段。与 --batch 一起使用”,因此这里有点题外话,但应在此处提及。

回答by ghostdog74

in bash, you use $()syntax.

在 bash 中,您使用$()语法。

#!/bin/bash
ret=$(mysql testdb -e "select count(*) from tbl where last_error_date < DATE_SUB(NOW(), INTERVAL 1  HOUR);")
if [[ "$ret" > 0 ]];then
   echo "there is count"
else
   echo "no count"
fi

回答by Wrikken

I usually do this:

我通常这样做:

 var=`mysql -e "SELECT COUNT(*) FROM ...\G" | awk '/COUNT/{print }/'`

回答by XDjuj

For my part I simply use grep -vto exclude the line printing count(*)from the return of MySQL.

就我而言,我只是用来从 MySQL 的返回中grep -v排除行打印count(*)

So I get the counter like that:

所以我得到这样的计数器:

db_name="NAME_DB";
db_user="USER_DB";
db_pwd="PWD_DB";
counter=`mysql -u${db_user} -p${db_pwd} ${db_name} -e "SELECT count(*) FROM my_table WHERE something = '1';" | grep -v "count"`;
echo "Count for request: $counter";

I use it for some Wordpress stuff this way, reading databases infos from the wp-config.phpfile:

我以这种方式将它用于一些 Wordpress 的东西,从wp-config.php文件中读取数据库信息:

wp_db_infos="wp-config.php";
wp_db=`cat ${wp_db_infos} | grep "DB_NAME" | awk -F ', ' '{print }' | awk -F "'" '{print }'`;
wp_user=`cat ${wp_db_infos} | grep "DB_USER" | awk -F ', ' '{print }' | awk -F "'" '{print }'`;
wp_pwd=`cat ${wp_db_infos} | grep "DB_PASSWORD" | awk -F ', ' '{print }' | awk -F "'" '{print }'`;
img_to_update=`mysql -u${wp_user} -p${wp_pwd} ${wp_db} -e "SELECT count(*) FROM wp_offres WHERE maj_img = '1';" | grep -v "count"`;

回答by Vladimir Perepechenko

local count=$(mysql -u root --disable-column-names --batch --execute "SELECT COUNT(*) FROM mysql.user WHERE user = '$DstDbName'")
    if [[ "$count" > 0 ]]
    then
    fi          

--batch - do clear output w/o borders --disable-column-names - prints only row with value

--batch - 不带边框清除输出 --disable-column-names - 仅打印带值的行

no creasy AWK used :)

没有使用任何皱巴巴的 AWK :)