【问题标题】:If Statement Issues如果语句问题
【发布时间】:2014-11-14 05:19:13
【问题描述】:

我是脚本新手,如果系统上有准备好更新,我会尝试给自己发送一封电子邮件。我想要这样做的方式是,如果 yum check-update 报告任何包,给我发一封电子邮件,如果没有,什么也不做。这是我想出的:

#!/bin/bash

cat /dev/null > /root/scripts/updates.txt

yum check-update > /root/scripts/updates.txt

updatecheck=$(cat /root/scripts/updates.txt | grep updates | grep -v mirrors)
updatestatus=$(echo "$updatecheck" | wc -l)

if [ $updatestatus == 0 ]
then
        mail -s "There are no updates for monitor01.aevhosting.com" email@address.com
else
        mail -s "There are updates for monitor01.aevhosting.com" email@address.com
fi

它没有做任何事情,它只是挂在那里,要返回命令行,我必须按 control+c 退出脚本。我也在跟踪邮件日志,看看它是否发送了任何东西,但事实并非如此。不知道这里出了什么问题,但非常感谢任何和所有的帮助!!

谢谢

【问题讨论】:

  • 第一行似乎没有必要;当您在下一行运行 yum 时,将创建该文件。
  • mail 命令是否适用于我们的命令行?
  • cat ... | grep 是一种反模式,grep 接受文件就好了,它不需要管道输入。
  • check-update 也为此目的返回一个退出代码。 0 是没有可用更新,1 表示错误,100 如果更新可用。
  • 您好,是的,当我在命令行中自行运行该邮件行时,邮件命令有效并发送电子邮件。我已经自己完成了每一行,并且一切都由它自己完成,但是当我将所有内容放在一起时,如您所见,它不会做任何事情并卡住。

标签: bash if-statement scripting


【解决方案1】:

mail 正在等待标准输入来填充电子邮件正文。管它的东西:

echo 'This is automated e-mail, do not reply' | mail -s "There are updates..." email@address.com

【讨论】:

    【解决方案2】:

    感谢大家根据@choroba 的建议为我提供的帮助。我必须将 echo "test" 放在我在下面执行的邮件命令之前,它可以工作:

    #!/bin/bash
    
    cat /dev/null > /root/scripts/updates.txt
    
    yum check-update > /root/scripts/updates.txt
    
    updatecheck=$(cat /root/scripts/updates.txt | grep updates | grep -v mirrors)
    updatestatus=$(echo "$updatecheck" | wc -l)
    
    if [ $updatestatus == 0 ]
    then
        echo "There are no updates for monitor01.aevhosting.com" | mail -s "Update Status" email@gmail.com
    else
        echo "There are updates for monitor01.aevhosting.com" | mail -s "Update Status" email@gmail.com
    fi
    

    再次感谢大家的帮助!!

    【讨论】:

    • 看起来你说的是你使用了choroba's answer。如果是这种情况,您应该接受它,而不是发布您自己的解决方案。
    • 如果是这样的话,我不是故意的。这是第一次使用该网站不知道该怎么做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多