我是Bash编程的新手,可能真的很傻。
基本上,我正在编写一段脚本来ping通传入的IP地址,它将从中获取传输的数据包,并根据丢失的数据包数量返回错误或通过消息。
但是,每当我从终端运行脚本时,都会不断收到消息-
./ipGatewayCheck.sh: line 13: [: missing
]'`
这是我的代码:
#!/bin/bash
healthy_status=0
warning_status=10
critical_status=100
for gateway in $@
do
RESULT=`ping -q -c 10 $gateway | grep 'packets transmitted' | awk '{print $6}' | tr -d "%"`
echo "$RESULT"
if [ $RESULT -eq $healthy_status ]; then
echo "No Issue - IP Address is pinging"
elif [ $RESULT -ge $warning_status && -le $critical_status ]; then
echo "Warning - Issue with packet loss on this IP Address"
elif [ $RESULT -eq $critical_status ]; then
echo "Critical - 100% packet loss on this IP Address"
fi
done
任何帮助将不胜感激。
您需要使用[[
和]]
才能&&
在方括号内使用:
if [[ "$RESULT" -eq "$healthy_status" ]]; then
echo "No Issue - IP Address is pinging"
elif [[ "$RESULT" -ge "$warning_status" && "$RESULT" -le "$critical_status" ]]; then
echo "Warning - Issue with packet loss on this IP Address"
elif [[ "$RESULT" -eq "$critical_status" ]]; then
echo "Critical - 100% packet loss on this IP Address"
fi
另外,您也可以在BASH中使用((
和))
:
if (( RESULT == healthy_status )); then
echo "No Issue - IP Address is pinging"
elif (( RESULT == warning_status && RESULT < critical_status )); then
echo "Warning - Issue with packet loss on this IP Address"
elif (( RESULT == critical_status )); then
echo "Critical - 100% packet loss on this IP Address"
fi
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句