如何修复我的Bash脚本?

时间:2013-12-02 07:01:24

标签: bash if-statement for-loop

我从我的脚本中获取的一些代码:

hh=$(bc -l <<< "scale=5; $h2-$h1")
delta=$(abs $hh)
echo "delta h = $delta"

for (( i=0; i <= ( ${#arr0[@]} - 1); i++ )); do
  if [ $delta > 0.03000000000000000000 ] && [ ${arr1[$i]} > 100 ] then 
    echo "attack Ddos"
    iptables -A INPUT -s 192.168.162.1 -j DROP
        break
  else
    echo "No attack Ddos"
  fi
done

其输出:

delta h = .05526800237680984201
./trung1 (copy): line 115: syntax error near unexpected token `else'
./trung1 (copy): line 115: `  else'

如何解决此错误?

2 个答案:

答案 0 :(得分:3)

then之前需要一个分号。在这方面,if-thenfor-do的工作方式相同。

答案 1 :(得分:2)

您只是在if声明后错过了分号:

hh=$(bc -l <<< "scale=5; $h2-$h1")
delta=$(abs $hh)
echo "delta h = $delta"

for (( i=0; i <= ( ${#arr0[@]} - 1); i++ )); do
  if [ $delta > 0.03000000000000000000 ] && [ ${arr1[$i]} > 100 ] ; then #There is either a semi-colon here, or a new line
    echo "attack Ddos"
    iptables -A INPUT -s 192.168.162.1 -j DROP
        break
  else
    echo "No attack Ddos"
  fi
done

更多阅读:Bash convention for if then