错误:需要整数表达式

错误:需要整数表达式

我编写了一个脚本来确定服务器上的平均负载,如下所示:

#!/bin/bash

loadavg=`top -b -n1 | grep -i load | awk -F, '{print$4}'| awk -F: '{print $2}'`

if [ "${loadavg}" -le  1 ]
then
echo "OK - Load Average = ${loadavg} | Load_Average=${loadavg}"
exit 0;
elif [ "${loadavg}" -gt 1 ] && [ "${loadavg}" -le 2 ]
then
echo "WARNING - Load Average = ${loadavg} | Load_Average=${loadavg}"
exit 1;
elif [ "${loadavg}" -gt 2 ]
then
echo "CRITICAL - Load Average = ${loadavg} | Load_Average=${loadavg}"
exit 2;
else
echo "UNKNOWN - Load Average = NaN | Load_Average=NaN"
fi

执行脚本时,显示以下错误:

./loadavg.sh
./loadavg.sh: line 5: [:  0.06: integer expression expected
./loadavg.sh: line 9: [:  0.06: integer expression expected
./loadavg.sh: line 13: [:  0.06: integer expression expected
UNKNOWN - Load Average = NaN | Load_Average=NaN

答案1

bashksh93(与or zsh1相反)不能进行浮点运算。awk不过可以,所以你可以在 中完成整个事情awk

另外,您不需要使用top(并等待 1 秒)来获取负载。获取负载的规范方法是来自uptime.

uptime | awk '{load=+$(NF-2)}
  load > 2 {print "CRITICAL: " load; exit 2}
  load > 1 {print "WARNING: " load; exit 1}
  {print "OK: " load; exit 0}
  END {if (!NR) {print "UNKNOWN"; exit 3}'
exit

1不过zsh,您需要使用(( loadavg > 2 ))代替[ "$loadavg" -gt 2 ]语法来比较非整数

答案2

top -b -n1 | grep -i load | awk -F, '{print$4}'| awk -F: '{print $2}'返回nada,因此你的错误。

答案3

您的loadavg值为空,会导致语法错误[

$ top -b n1 | grep -i load | awk -F, '{print$4}'| awk -F: '{print $2}'
<blank line here>

您必须将其更改为:

$ top -b n1 | grep -i load | awk -F, '{print$4}'| awk -F: '{print $1}'
 0.24

但是,您应该在脚本中使用更新的测试,它可以解决这个问题:

$ [[ "" -gt 1 ]] || echo "OK"
OK

与较旧的[

$ [ "" -gt 1 ] || echo "OK"
bash: [: : integer expression expected
OK

更新

bash无法处理浮点数,因此您的比较(即使使用新测试[[..]])也会显示错误。

您可以使用其他工具来完成此任务,例如bcawk...

例子:

$ [[ $(echo "3 > 1" | bc -l) == 1 ]] && echo OK
OK
$[[ $(echo "3 < 1" | bc -l) == 1 ]] || echo Not OK
Not OK

答案4

首先,运行top比仅仅获取“正常运行时间”更昂贵。

$ uptime
 16:15:38 up 6 days, 23:22, 23 users,  load average: 0.99, 0.82, 0.70

其次,正如 @Stéphane Chazelas 提到的,bash 不喜欢浮点运算。

$ [[ "0.1" -lt 1 ]] 
bash: [[: 0.1: syntax error: invalid arithmetic operator (error token is ".1")

幸运的是,bash 扩展可以解决这个问题。从男人狂欢:

   ${parameter%word}
   ${parameter%%word}
         Remove matching suffix pattern.  The word is expanded to produce a pattern just as in pathname expansion.   If
         the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is
         the expanded value of parameter with the shortest matching pattern (the ``%'' case) or  the  longest  matching
         pattern  (the  ``%%'' case) deleted.  If parameter is @ or *, the pattern removal operation is applied to each
         positional parameter in turn, and the expansion is the resultant list.  If parameter is an array variable sub-
         scripted  with  @  or *, the pattern removal operation is applied to each member of the array in turn, and the
         expansion is the resultant list.

因此用“%.*”去掉小数部分变得非常容易

$ la=`uptime | sed 's/.*average: \([0-9\.]*\).*/\1/'`
$ if [ ${la%.*} -lt 1 ]; then echo "OK - Load average is $la"; fi
OK - Load average is 0.42

相关内容