壳牌 - 比小于双倍

时间:2012-12-19 17:39:20

标签: shell

#!/bin/bash

# Obtain the server load
loadavg=`uptime |cut -d , -f 4|cut -d : -f 2`
thisloadavg=`echo $loadavg|awk -F \. '{print $1}'`

if [ "$thisloadavg" -eq "0.01" ]; then

ulimit  -n 65536
service nginx restart
service php-fpm restart

fi

错误是:

./loadcheck.sh: line 7: [: 0.01: integer expression expected

我想做一个loadcheck shell脚本,它可以比较double而不是integer,因为我希望确保负载返回小于0.01,即0.00,

如果我使用0,即使加载为0.05,它仍然会执行代码。

2 个答案:

答案 0 :(得分:1)

在zsh中你可以简单地使用:

if [[ "$thisloadavg" < "0.01" ]]; then

double [[ construct 允许额外的测试,而在zsh中它允许浮点测试。

答案 1 :(得分:0)

Bash无法处理浮点值,因此您需要使用其他命令,例如awkexprbc来执行此操作。例如,使用bc

loadavg=$(uptime | cut -d, -f4 |cut -d: -f2)
low_load=$(echo "$loadavg < 0.01" | bc -l)
if [ $low_load -eq 1 ]; then
  # do stuff
fi