Shell for循环,停止声明

时间:2016-11-08 20:37:14

标签: linux shell for-loop

我试图写一个从1到10的for循环,然后计算(1到10 mod 5)+ 2.之后我想像这样显示它(1到10 mod 5)+ 2 =回答。但是,我在循环开始时收到错误,这是一个语法错误。

for (( i = 0; i <= 10; i++)); do
    calculate=(i % 5) + 2
    echo ("("i "% 5) + 2" calculate)
done

1 个答案:

答案 0 :(得分:1)

尝试以下更改:

calculate=$(( i % 5 + 2 ))         
  # $(( ... )) is the shell's way to do arithmetic
echo "($i % 5) + 2 = " $calculate  
  # $x is a way to refer to the value of variable x 
  # (also inside a double-quoted string)

for循环标题实际上没问题。