【问题标题】:Issue in the shell script sending email发送电子邮件的 shell 脚本中的问题
【发布时间】:2021-05-28 14:13:53
【问题描述】:

我正在尝试根据记录数检查表格并发送电子邮件。如果特定日期的记录为零,则将发送成功电子邮件。如果特定日期有任何记录,则错误消息应该消失。 我的问题是 - 正在发生相反的情况。即使没有记录,也会发出错误邮件。

下面是脚本


#!/bin/bash
export PGPASSWORD=xyz
TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
mailid='xyz@gmail.com'

cd /opt/postgres/pgsql/bin

vartest=`./psql -U user -h  host -p port umrm -t -c "select count(*) from dataprocess_errors where error_date = current_date"` >> /opt/rmapp/test_error.log

if [$vartest -eq 0]
then

echo 'Files processed without errors'  | mutt -s "Processing Success `date`" $mailid $attached  >> /opt/rmapp/test_error.log

else

error=`./psql -U user -h  host -p port umrm-t -c "select count (*) from dataprocess_errors where error_date = current_date and error_message like '%More than 25 employees%';"` >> /opt/rmapp/test_error.log

if [$error -eq 0]
        then

        echo 'Please check the table and fix the errorneous records'  | mutt -s "Alert!! $vartest records failed in processing  `date`" $mailid $attached  >> /opt/rmapp/test_error.log

        else

        echo 'Please check the table'  | mutt -s "Alert!!! processing stopped  `date`" $mailid $attached  >> /opt/rmapp/test_error.log
        fi

fi

echo "Error Check completed at :  $TIMESTAMP" >>  /opt/rmapp/test_error.log

else 部分中的第二个 if 条件是根据变量“error”的输出来识别不同类型的错误。

我是 shell 脚本的菜鸟,所以请帮忙。 提前感谢所有帮助。

【问题讨论】:

  • 您的连接字符串在此问题中可见。 :O
  • 这是一个虚拟连接字符串:D
  • 将您的代码粘贴到shellcheck.net 以获得语法错误帮助。
  • 感谢帮助

标签: postgresql shell


【解决方案1】:

考虑到您的psql 命令运行良好,您在上述脚本中只有一个问题。在两个 if 条件下,'[' 之后和 ']' 之前的空格都缺失。 所以你的if 条件应该是这样的:

if [ $vartest -eq 0 ]

if [ $error -eq 0 ]

【讨论】:

    【解决方案2】:

    在你的台词中

    vartest=`./psql -U rmdbprd -h  atla-db-rmgr-prd-priv.com -p 5432 umrm -t -c "select count(*) from dataprocess_errors where error_date = current_date"`
    

    error=`./psql -U rmdbprd -h  atla-db-rmgr-prd-priv.com -p 5432 umrm-t -c "sele ........
    

    您从命令中获取标准 - 而不是错误代码

    获取错误代码你可以(例如):

    ./psql -U rmdbprd -h  atla-db-rmgr-prd-priv.com -p 5432 umrm -t -c "select count(*) from dataprocess_errors where error_date = current_date"
    if [ $? -ne 0 ];
    then
      echo "there was an error on above command"
    fi
    

    注意:if 必须紧跟在您正在测试错误代码的命令之后

    参考号:https://en.wikipedia.org/wiki/Bash_(Unix_shell)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-25
      • 2010-12-19
      • 2011-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-05
      • 2011-09-16
      相关资源
      最近更新 更多