![如何对以下代码的内存值进行百分比四舍五入?](https://linux22.com/image/162962/%E5%A6%82%E4%BD%95%E5%AF%B9%E4%BB%A5%E4%B8%8B%E4%BB%A3%E7%A0%81%E7%9A%84%E5%86%85%E5%AD%98%E5%80%BC%E8%BF%9B%E8%A1%8C%E7%99%BE%E5%88%86%E6%AF%94%E5%9B%9B%E8%88%8D%E4%BA%94%E5%85%A5%EF%BC%9F.png)
do 主机名=$(ssh ${i} 主机名-s)
memory=$(ssh ${i} free -t | awk 'FNR == 2 {printf(" %.2f%"), $3/$2*100}')
mem_health=$(ssh ${i} free -t | awk 'FNR == 2 {printf(" %.2f%"), $3/$2*100}')
if [ ${mem_health} -ge 80 ];
then
mem_health="CRITICAL"
else
mem_health="NORMAL"
fi
答案1
使用
mem_health=$(ssh ${i} free -t | awk 'FNR == 2 {printf(" %d"), $3/$2*100}')
即使用%d
而不是%.2f%
因为您想要一个整数并且不希望在 mem_health 变量中使用百分号。