在我的用于服务器监视的自定义bash脚本中,它实际上是为了迫使我的CentOS服务器执行一些操作,并在资源重载时间超过预期时提醒我,我得到以下错误
第17行:[:5.74:预期整数表达式*
现在,根据定义,所有iostat的结果都是浮点数,我已经在iostat命令中使用了awk (等待),那么我如何使bash脚本期望一个而不是整数呢?
**值5.74表示当前iostat结果
#!/bin/bash
if [[ "`pidof -x $(basename $0) -o %PPID`" ]]; then
# echo "Script is already running with PID `pidof -x $(basename $0) -o %PPID`"
exit
fi
UPTIME=`cat /proc/uptime | awk '{print $1}' | cut -d'.' -f1`
WAIT=`iostat -c | head -4 |tail -1 | awk '{print $4}' |cut -d',' -f1`
LOAD=`cat /proc/loadavg |awk '{print $2}' | cut -d'.' -f1`
if [ "$UPTIME" -gt 600 ]
then
if [ "$WAIT" -gt 50 ]
then
if [ "$LOAD" -gt 4 ]
then
#action to take (reboot, restart service, save state sleep retry)
MAIL_TXT="System Status: iowait:"$WAIT" loadavg5:"$LOAD" uptime:"$UPTIME"!"
echo $MAIL_TXT | mail -s "Server Alert Status" "mymail@foe.foe"
/etc/init.d/httpd stop
# /etc/init.d/mysql stop
sleep 10
# /etc/init.d/mysql start
/etc/init.d/httpd start
fi
fi
fiCentOS版本6.8 (最终) 2.6.32-642.13.1.el6.x86_64
发布于 2017-02-10 17:00:40
通常,您需要使用本机shell数学以外的其他内容,如BashFAQ #22中所描述的。但是,由于您比较的是整数,这很容易:您可以在小数点处截断。
[ "${UPTIME%%.*}" -gt 600 ] # truncates your UPTIME at the decimal point
[ "${WAIT%%.*}" -gt 50 ] # likewisehttps://stackoverflow.com/questions/42164706
复制相似问题