如何使用shell脚本在变量中获取内存使用情况

时间:2015-11-18 07:38:58

标签: shell memory

我使用以下脚本来获取内存使用值,我想在变量中使用此值,以便我可以对该值应用“if”条件。请建议

@echo off
free -m | awk 'NR==2{printf "Memory Usage: %s/%sMB (%.2f%)\n", $3,$2,$3*100/$2 }'

1 个答案:

答案 0 :(得分:2)

你可能想要

memory_usage=`free -m | awk 'NR==2{print $3*100/$2 }'`
if [ `echo "${memory_usage} > 90.0" | bc` -eq 1 ] ; then
    echo " > 90.0 "
else
    echo " <= 90.0 "
fi

注意如何将命令结果保存到变量中,以及如何比较浮点数。