#!/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,它仍然会执行代码。
答案 0 :(得分:1)
答案 1 :(得分:0)
Bash无法处理浮点值,因此您需要使用其他命令,例如awk
,expr
或bc
来执行此操作。例如,使用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