【问题标题】:Redirection of MySQL Queries into a log/text file in shell script and read the file for particular string将 MySQL 查询重定向到 shell 脚本中的日志/文本文件并读取特定字符串的文件
【发布时间】:2013-05-15 09:24:14
【问题描述】:


我编写了一个 shell 脚本 MySQL_Test.sh 从 linux shell 连接到 MySQL 数据库并执行三个不同的 select 语句,如下所示,

Select count(*) as My_Column_Name from <table_name> where <condition>

我的 shell 脚本如下所示.

mysql -u$MASTER_DB_USER -p$MASTER_DB_PASSWD -P$MASTER_DB_PORT -h$MASTER_DB_HOST <<EOF
$BS_Query 
$Exp_Query
$ROI_Query
EOF

现在我的要求是,
1. 我想将上述三个查询的输出重定向到一个文本或日志文件中。
2. 如果任何查询返回 > 0 行,请发送电子邮件。
3. 发送电子邮件时附上文本/日志文件。

任何人都可以帮助我修复 1 和 2。在此先感谢。

【问题讨论】:

    标签: mysql linux shell


    【解决方案1】:
    mysql -u$DB_USER -p$PASSWD -P$DB_PORT -h$DB_HOST <<EOF > output.txt
    $BS_Query 
    $Exp_Query
    $ROI_Query
    EOF
    
    # For each even line (i.e. containing the count(*) result), test if count(*) != 0
    line_number=1
    for line in `cat output.txt`; do
      if [ $((line_number % 2)) -eq 0 ]; then
        if [ $line -ne 0 ]; then
          `( echo "Here is your report"; uuencode output.txt output.txt ) | mail -s "SQL report" admin@sqldb`
          exit
        fi
      fi
      line_number=$((line_number + 1))
    done
    

    【讨论】:

    • 嗨 Sylvain... 非常感谢您... 它对我有用... 我们可以将 output.txt 也附加到邮件的同一行吗???
    • 你说你只想要 1 和 2 :)。要附加生成的文件,您可以使用:( echo "Here is your report"; uuencode output.txt output.txt ) | mail -s "SQL report" admin@sqldb。更多解释here
    • 我已经用这个命令编辑了我的答案以获得完整的脚本。
    猜你喜欢
    • 2014-11-08
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 2020-05-28
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    相关资源
    最近更新 更多