我創建了一個 .sh 檔案來監視 2 個檔案路徑并將磁盤大小發送給我。它運行,我沒有收到郵件。并且檔案系統 >90%
#!/bin/bash
used=$(df -Ph | grep 'location1' | awk {'print $5'})
used1=$(df -Ph | grep '/location2' | awk {'print $5'})
max=80%
if [ ${used%?} -gt ${max%?} ]; then mail -s 'Disk space alert' [email protected];[email protected] << EOF
The Mount Point "location1" on $(hostname) has used $used at $(date);
if [ ${use1%?} -gt ${max%?} ]; then mail -s 'Disk space alert' [email protected]; [email protected] << EOF
The Mount Point "location2" on $(hostname) has used $used1 at $(date);
EOF
fi
uj5u.com熱心網友回復:
謝謝大家,我已經能夠弄清楚了。
#!/bin/bash
used=$(df -Ph | grep 'location1' | awk '{print $5}' | sed 's/%//g' )
used1=$(df -Ph | grep 'location2' | awk '{print $5}' | sed 's/%//g' )
max=80%
if [ ${used%?} -gt ${max%?} ]; then
if [ ${use1%?} -gt ${max%?} ]; then
mail -s 'Disk space alert' [email protected] [email protected] << EOF
The Mount Point 'location2' on $(hostname) has used $used1 at $(date);
EOF
fi
fi
uj5u.com熱心網友回復:
您未能包含EOF標記,因此其余代碼隱藏在 here 檔案中。(您問題中的語法著色應該可以幫助您注意。)
順便說一句,您希望避免無用grep并修復縮進。我也猜你不希望第二個if只在第一個觸發時觸發。
#!/bin/bash
used=$(df -Ph | awk '/location1/ { print $5}')
used1=$(df -Ph | awk '/\/location2/ { print $5}')
max=80%
if [ ${used%?} -gt ${max%?} ]; then
mail -s 'Disk space alert' [email protected];[email protected] <<__EOF
The Mount Point "location1" on $(hostname) has used $used at $(date);
__EOF
fi
if [ ${use1%?} -gt ${max%?} ]; then
mail -s 'Disk space alert' [email protected]; [email protected] <<__EOF
The Mount Point "location2" on $(hostname) has used $used1 at $(date);
__EOF
fi
具有較少代碼重復的更慣用的解決方案將回圈引數。
#!/bin/bash
max=80
for mountpoint in location /location2; do
used=$(df -Ph "$mountpoint" | awk 'NR>1 { print $5}')
if [ ${used%?} -gt $max ]; then
mail -s 'Disk space alert' [email protected];[email protected] <<____EOF
The Mount Point "$mountpoint" on $(hostname) has used $used at $(date);
____EOF
fi
done
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/312934.html
上一篇:如何指定自定義發件人?
