Shell脚本无法正常工作

时间:2018-06-27 19:11:05

标签: linux shell awk scripting

cpu= $(mpstat | awk '$12 ~ /[0-9.]+/ { printf("%d%%",100 - $12) }' | cut -d. -f1)

如何限制数字输出?

%字符串与数字进行比较失败。

1 个答案:

答案 0 :(得分:1)

请勿将%放在awk输出中。创建电子邮件时将其添加。您还可以使用awk的int()函数从输出中除去分数,而不是通过管道传递到cut

还要确保=分配中cpu周围没有空格。

cpu=$(mpstat | awk '$12 ~ /[0-9.]+/ {print int(100 - $12) }')
if (( $cpu > 5 ))
then mail -s "CPU Utilization monitoring" "xxx@yyy.com" <<EOF
CPU Utilization is exceeded in Ec2-user Server.
Current use is $cpu%.
EOF
fi