我有這段代碼,它基本上在 DF 命令中執行一個回圈,以查看哪些磁盤已滿 90% 以上。
df -H | sed 1d | awk '{ print $5 " " $1 }' | while read -r dfh;
do
#echo "$output"
op=$(echo "$dfh" | awk '{ print $1}' | cut -d'%' -f1 )
partition=$(echo "$dfh" | awk '{ print $2 }' )
if [ $op -ge 90 ];
then
echo ">> ### WARNING! Running out of space on \"$partition ($op%)\" on $(hostname) as on $(date)" >> LOGFILE
echo -e ">> ### WARNING! Running out of space on \"$partition ($op%)\" on $(hostname) as on $(date)"
echo ">> There is not enough left storage in the disk to perform the upgrade, exiting..." >> LOGFILE
echo -e ">> There is not enough left storage in the disk to perform the upgrade, exiting..."
exit 1
elif [ $op -ge 85 ];
then
echo -e ">> ### WARNING! Running out of space on \"$partition ($op%)\" on $(hostname) as on $(date)" >> LOGFILE
echo ">> ### WARNING! Running out of space on \"$partition ($op%)\" on $(hostname) as on $(date)"
echo ">> There enough left storage in the disk to perform the upgrade, but it is recommended to first increase the size of the disk $partition" >> LOGFILE
echo -e ">> There enough left storage in the disk to perform the upgrade, but it is recommended to first increase the size of the disk $partition"
fi
done
if [ "$?" -eq 1 ];
then
exit
else
echo -e ">> There is enough left storage in the disk to continue with the upgrade, OK"
fi
我希望它僅在至少一個磁盤已滿 90% 時才退出,這是最后一個 if 陳述句的目的
這里的問題是回圈退出第一個磁盤上的識別率超過 90%,這很糟糕,因為如果我有 3 個磁盤超過 90%,它只會報告其中一個(回圈中的第一個)然后出口。基本上,我希望腳本報告所有處于 90% 或更多的磁盤(以及那些也處于 85% 但沒有退出的磁盤,如您所見)。
這可能嗎?先感謝您
uj5u.com熱心網友回復:
您可以在一次檢查中完成,如下所示:
$ a=10 b=5 c=7 ; (( a>=10 && b>=10 && c>=10 )) && echo 'all >= 10'
$ a=10 b=5 c=10; (( a>=10 && b>=10 && c>=10 )) && echo 'all >= 10'
$ a=10 b=10 c=10; (( a>=10 && b>=10 && c>=10 )) && echo 'all >= 10'
all >= 10
uj5u.com熱心網友回復:
您的腳本看起來像是在大聲尖叫要重構為 Awk。但這里有一個更溫和的重構。
rc=0
df -H |
awk 'NR>1 { n=$5; sub(/%/, "", n); print n, $1 }' |
while read -r op partition;
do
if [ $op -ge 90 ];
then
tee -a LOGFILE <<-________EOF
>> ### WARNING! Running out of space on "$partition ($op%)" on $(hostname) as on $(date)
>> There is not enough left storage in the disk to perform the upgrade, exiting...
________EOF
rc=1
elif [ $op -ge 85 ];
then
tee -a LOGFILE <<-________EOF
>> ### WARNING! Running out of space on "$partition ($op%)" on $(hostname) as on $(date)
>> There enough left storage in the disk to perform the upgrade, but it is recommended to first increase the size of the disk $partition
________EOF
fi
[ "$rc" -eq 0 ]
done &&
echo ">> There is enough left storage in the disk to continue with the upgrade, OK" ||
exit 1
這會在回圈所有磁區時跟蹤rc,然后僅在回圈完成時繼續執行最終條件。
一般來說,避免echo -e支持printf,雖然在這里,因為-e無論如何都沒有做任何有用的事情,所以這里的檔案效果更好。
uj5u.com熱心網友回復:
您可以執行以下操作:
#!/bin/sh
warn=${1-85}
fail=${2-90}
test "$warn" -lt "$fail" || { echo Invalid parameters >&2; exit 1; }
check_disk(){
op=${1%%%}
partition=${2}
if test "$op" -ge "$warn"; then
tee -a LOGFILE <<-EOF
>> ### WARNING! Running out of space on "$partition ($op%)" on $(hostname) as on $(date)
>> There is not enough left storage in the disk to perform the upgrade, exiting...
EOF
fi
if test "$op" -ge "$fail"; then
return 1
fi
} 2> /dev/null
rv=0
df -H | awk 'NR > 1{ print $5 " " $1 }' \
| { while read -r op partition; do
if ! check_disk "$op" "$partition"; then rv=1; fi
done
test "$rv" -eq 0
} || exit 1
幾點注意事項:
如果要<<-抑制輸出中的縮進,則必須使用文字硬制表符作為縮進。
您需要將 放在while/done括號中以使rv變數具有能夠檢查它的完整范圍。如果您只是這樣做df ... | while do/done,那么該變數將在管道之外取消設定。
這仍然有點笨拙,最好是完成整個事情awk而不是while/do在 shell 中使用回圈,但這些是一些想法。特別是,您絕對不想在同一echo行中寫 4 次!
另請注意,決議 的輸出df非常脆弱。在我的盒子里,有幾行檔案系統列包含空格,所以第 5 列不是掛載點的當前使用情況。此腳本在此類輸出上可能無法正常作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/521561.html
標籤:linux重击壳
上一篇:更改awk中的上一個重復行
