【问题标题】:Modifying bash script including MySQL query to determine if query returns content or no result修改包含 MySQL 查询的 bash 脚本以确定查询是否返回内容或没有结果
【发布时间】:2021-03-08 09:23:31
【问题描述】:

希望有人能提供帮助。

我有一个现有的 bash 脚本,它每周发送一封电子邮件,列出添加到我的 CMS 的所有新文档:

#!/bin/bash
DATE=$(date +"%d-%b-%Y")
RECIPIENTS=***@***.com
MAILBODY=$(</root/bin/mailbody.txt)
BCC="***@***.com"
SENDER="***@***.com"
SUBJECT="New Documents Released Week Ending "
GREETING="Good morning,"
MAILBODY=$MAILBODY
#MAILBODY="The following content has been added or updated in the Content Management System in the last 7 days (if no files are listed, it means there have been no updates):"
CLOSEBODY="This is an automated email sent by the **** Content Management System (https://***.****.***:****). If you have any problems accessing the content listed in this email, please email ***@****.com"

echo "FROM:" $SENDER > /root/bin/mailtext
echo "TO:" $RECIPIENTS >> /root/bin/mailtext
echo "BCC:" $BCC >> /root/bin/mailtext
echo "SUBJECT:" $SUBJECT $DATE >> /root/bin/mailtext
echo "" >> /root/bin/mailtext
echo $GREETING >> /root/bin/mailtext
echo "" >> /root/bin/mailtext
echo $MAILBODY >> /root/bin/mailtext
echo "" >> /root/bin/mailtext
echo "SELECT name AS NAME,MIN(id) as ID,MIN(created_at) as CREATED,MIN(CONCAT('https://***.***.com:****/link/',id)) as URL from pages WHERE deleted_at IS NULL AND name NOT LIKE 'AO%' AND name NOT LIKE 'KB%' AND name NOT LIKE 'Odoo%' AND name NOT LIKE 'WK%' AND draft=0 AND created_at > DATE_SUB(NOW(), INTERVAL 7 DAY) GROUP BY name \G" | mysql -u root -D  bookstack >> /root/bin/mailtext
echo "" >> /root/bin/mailtext
echo "" >> /root/bin/mailtext
echo $CLOSEBODY >> /root/bin/mailtext

cat /root/bin/mailtext | /usr/sbin/sendmail -t

但是,我想进一步个性化这个脚本,这样如果查询没有返回结果,它会将电子邮件的正文(包含在 $MAILBODY 中)更改为如下内容:

过去 7 天没有新文档添加到 CMS。

我尝试了很多不同的方法来尝试实现我想要的东西,但都失败了(我对 bash 知之甚少,对 SQL 更是知之甚少!)。我试图创建一个 IF、THEN、ELSE 构造,但我似乎无法找到一种方法来检测查询何时没有返回结果。我最后一次尝试是这样,但显然它不起作用:

TEST=$(echo "SELECT IFNULL(SELECT name AS NAME,MIN(id) as ID,MIN(created_at) as CREATED,MIN(CONCAT('https://***.****.***:****/link/',id)) as URL from pages WHERE deleted_at IS NULL AND name NOT LIKE 'AO%' AND name NOT LIKE 'KB%' AND name NOT LIKE 'Odoo%' AND name NOT LIKE 'WK%' AND draft=0 AND created_at > DATE_SUB(NOW(), INTERVAL 1 DAY) GROUP BY name \G")" | mysql -u root -D  bookstack) 
IF (echo "IS NULL $TEST") THEN
$MAILBODY = "There have been no new documents added to the CMS in the last 7 days."
fi

谁能帮我修改我的脚本来实现我的需要?

感谢所有帮助!

标记

【问题讨论】:

  • 你是如何执行查询的?您通常会在查询前加上 mysql --username ... --password --- -e "SELECT ...."

标签: mysql sql linux bash


【解决方案1】:

将 SQL 查询的输出放入变量 v.gr。查询。测试该变量包含的字符串,根据结果执行命令

那么,试试吧:

#!/bin/bash
DATE=$(date +"%d-%b-%Y")
RECIPIENTS=***@***.com
MAILBODY=$(</root/bin/mailbody.txt)
BCC="***@***.com"
SENDER="***@***.com"
SUBJECT="New Documents Released Week Ending "
GREETING="Good morning,"
MAILBODY=$MAILBODY
#MAILBODY="The following content has been added or updated in the Content Management System in the last 7 days (if no files are listed, it means there have been no updates):"
CLOSEBODY="This is an automated email sent by the **** Content Management System (https://***.****.***:****). If you have any problems accessing the content listed in this email, please email ***@****.com"
SQLQUERY=$(echo "SELECT name AS NAME,MIN(id) as ID,MIN(created_at) as CREATED,MIN(CONCAT('https://***.***.com:****/link/',id)) as URL from pages WHERE deleted_at IS NULL AND name NOT LIKE 'AO%' AND name NOT LIKE 'KB%' AND name NOT LIKE 'Odoo%' AND name NOT LIKE 'WK%' AND draft=0 AND created_at > DATE_SUB(NOW(), INTERVAL 7 DAY) GROUP BY name \G" | mysql -u root -D  bookstack)

echo "FROM:" $SENDER > /root/bin/mailtext
echo "TO:" $RECIPIENTS >> /root/bin/mailtext
echo "BCC:" $BCC >> /root/bin/mailtext
echo "SUBJECT:" $SUBJECT $DATE >> /root/bin/mailtext
echo "" >> /root/bin/mailtext
echo $GREETING >> /root/bin/mailtext
echo "" >> /root/bin/mailtext
echo $MAILBODY >> /root/bin/mailtext
echo "" >> /root/bin/mailtext
if [ -z "$SQLQUERY" ] ; then # SQLQUERY contains an empty string.
  echo "There have been no new documents added to the CMS in the last 7 days." >> /root/bin/mailtext
else
  echo "$SQLQUERY" >> /root/bin/mailtext
fi 
echo "" >> /root/bin/mailtext
echo "" >> /root/bin/mailtext
echo $CLOSEBODY >> /root/bin/mailtext

cat /root/bin/mailtext | /usr/sbin/sendmail -t

由于您没有像往常一样在 SO 上提供最小的工作示例,因此我无法调试我提供给您的代码。您可能必须根据您的配置调整上面的“if”测试。

【讨论】:

  • 这个丑陋的代码可以通过多种方式美化...
  • 谢谢 - 将 ENDIF 语句更改为 FI 后效果很好。再次感谢皮埃尔
  • @MarkLees:哎呀,当然。我很抱歉...在上面编辑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多