2016-11-08 89 views
-2

我想寫一個for循環,從1到10,然後計算(1到10模5)+ 2.之後,我想要顯示它像這樣(1到10模5)+ 2 =答案。但是,我在循環開始時出現了一個語法錯誤。殼for循環,在聲明停止

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

Linux不是一種編程語言。 –

回答

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循環頭實際上是確定。

+0

這是shell,不是Perl或PHP,你不需要';'在每行的末尾。 – Barmar

+0

@Barmar你是對的,我從具有這些分號的單線版本複製/粘貼。 –