0

我正在测试一个变量是否大于另一个变量。无论值是什么,if 评估都会获得相同的值。

COMP(){
avg=$(for avg in $(for file in $(ls /var/log/sa/sa[0123]*); do echo $file; done); do sar -r -f $avg| tail -1; done | awk '{totavg+=$4} END {print (totavg/NR)*5}');
for comp in $(sar -r -f /var/log/sa/sa08 | egrep -v "^$|Average|CPU|used" | awk '{print $5}'); do
        if [ `echo $avg` <  `echo $comp` ];
                then echo 'You have had a spike!';
                echo "COMP = $comp";
                echo "AVG = $avg";
        fi;
done }

即使这些值并没有真正评估为真,我也得到了这个输出。

You have had a spike!
COMP = 41.20
AVG = 145.438
You have had a spike!
COMP = 41.20
AVG = 145.438
You have had a spike!
COMP = 41.19
AVG = 145.438
You have had a spike!  
COMP = 41.24 
AVG = 145.438

我已经尝试了多种方法,但无法使其正常工作。有任何想法吗?

4

3 回答 3

1

是的,正如 ingnacio 指出的那样

average=`echo $avg`;
comp1=`echo $comp`

  if ((average)) 2>/dev/null; then
     average=$((average))
   else
     average=0;
  fi
 if ((comp1)) 2>/dev/null; then
     comp1=$((comp1))
  else
     comp1=0;
  fi

if [ $average -lt $comp1 ];then
于 2012-09-09T16:48:31.817 回答
1

<按字典顺序比较。如果要比较整数,请使用-lt. 如果要比较浮点数,请使用bc而不是test.

于 2012-09-09T16:42:14.840 回答
0

您是在寻找数值比较还是文字(字符串)比较?可能需要不同的运算符,具体取决于哪个运算符。

于 2012-09-09T16:42:50.237 回答