我有下面的代碼,它不會發送grep輸出到電子郵件,但它顯示輸出命令行。不知道我做錯了運行bash命令後沒有收到grep輸出的郵件
sudo tail -f /path/to/file | while read line ; do
egrep 'successfully started \[[0-9] out of [0-9]\] components|successfully started \[[0-9] out of [0-9]\] components'
if [ $? -eq 0 ]; then
(echo " RESTARTS"; echo; echo $line) | mail -s "RESTART" [email protected]
else
echo " FAILS"
fi
done
[更新]
還有一種辦法,我想這樣做,但它不會發送email..I只是確保我能擺脫循環,不會永遠持續下去,因此這就是爲什麼我把尾巴而
while read line; do
echo "$line" | sudo tail -f /opt/bmc/ao/cdp/tomcat/logs/grid.log
if [ $(echo "$line" | grep -E 'successfully started \[[0-9] out of [0-9]\] components|successfully started \[[0-9] out of [0-9]\] components' | wc -l) -ne 0 ]; then
(echo "CDP RESTARTS"; echo; echo "$line") | mail -s "BAOCDP AUTO RESTART" [email protected]
break
fi
done
取出(腳本 –
OK,我會嘗試的 –
@RamanSailopal這將無法正常工作,只有'echo $ line'會被傳送到'mail',而前兩個回聲會進入stdout – Aaron