2014-09-19 42 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