2014-09-19 40 views
0

我不熟悉脚本,如果系统上有更新,我正在尝试向自己发送电子邮件。我想这样做的方式是,如果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 protected] 
else 
     mail -s "There are updates for monitor01.aevhosting.com" [email protected] 
fi 

它没有做任何事情,它只是挂在那儿,并回到命令行我要控制+ c退出脚本。我还拖尾邮件,看看它是否发出任何东西,但事实并非如此。不知道这里有什么问题,但任何和所有的帮助将非常满意!

谢谢

+1

第一行似乎没有必要;该文件将在下一行运行'yum'时创建。 – chepner 2014-09-19 15:00:49

+0

'mail'命令是否适用于我们的命令行? – anubhava 2014-09-19 15:02:11

+0

'猫... | grep'是一种反模式,'grep'接受文件就好了,它不需要管道输入。 – 2014-09-19 15:03:31

回答

2

mail等待标准输入,填补了电子邮件正文。管道的东西:

echo 'This is automated e-mail, do not reply' | mail -s "There are updates..." [email protected] 
0

谢谢大家的帮助,我得到了它的工作与@choroba的建议。我有我下面做的邮件命令之前把回声“测试”和它的作品:

#!/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 protected] 
else 
    echo "There are updates for monitor01.aevhosting.com" | mail -s "Update Status" [email protected] 
fi 

再次感谢您的帮助大家从!

+0

它看起来像你说的是你使用[choroba的答案](http://stackoverflow.com/a/25937065/2088135)。如果是这样,你应该接受它,而不是发布你自己的解决方案。 – 2014-09-19 15:41:05

+0

如果是这样,我并不是故意这样做。这是第一次使用该网站不知道该怎么做。 – mvelez83 2015-03-21 05:43:23