Bash 如果shell脚本中的for循环中的if循环不起作用
我有下面的shell脚本来发送电子邮件警报。当我只有for循环时,一切正常,但当我尝试添加if条件时,它似乎不起作用,每次都返回空消息体。脚本中的一个片段如下所示:Bash 如果shell脚本中的for循环中的if循环不起作用,bash,shell,if-statement,for-loop,Bash,Shell,If Statement,For Loop,我有下面的shell脚本来发送电子邮件警报。当我只有for循环时,一切正常,但当我尝试添加if条件时,它似乎不起作用,每次都返回空消息体。脚本中的一个片段如下所示: for host in $servers do connections=`ssh $host "netstat -a | grep ES | wc -l"` echo "$connections" >> debug.log echo "$host" >> debug.log if [$connections
for host in $servers
do
connections=`ssh $host "netstat -a | grep ES | wc -l"`
echo "$connections" >> debug.log
echo "$host" >> debug.log
if [$connections -gt 0]
then
echo "------------------------------------------------------------------" >> $emailmessage
echo "Host: $host needs to be checked" >> $emailmessage
echo "------------------------------------------------------------------" >> $emailmessage
echo "$connections" >> $emailmessage
else
echo "Everything is fine"
fi
done
当我尝试执行脚本时,总是出现第21行:[49:command not found
错误。我尝试过调试,但我发现我的脚本甚至没有进入if循环
有人能告诉我我错过了什么吗
提前谢谢
/rd在
[
和]
周围应该有空格,而且最好在引号中加上变量,所以根据需要更新行
...
if [ "$connections" -gt 0 ] ;
then
...
术语诡辩:if语句不是循环。@rond作为未来的提示,会自动指出这样的愚蠢错误。