我是 Bash 编程的新手,可能真的很傻。
基本上我正在编写一段脚本,它将对我传入的 IP 地址执行 ping 操作,它将从中获取传输的数据包,并根据丢失的数据包数量返回错误或通过消息。
但是,每当我从终端运行脚本时,我都会收到消息 -
./ipGatewayCheck.sh:第 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
关于BASH 语法错误 - [ : missing `]' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25991281/