Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如果shell脚本中的for循环中的if循环不起作用_Bash_Shell_If Statement_For Loop - Fatal编程技术网

Bash 如果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

我有下面的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 -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作为未来的提示,会自动指出这样的愚蠢错误。