gpt4 book ai didi

linux - Shell for 循环,在声明处停止

转载 作者:太空宇宙 更新时间:2023-11-04 05:43:05 25 4
gpt4 key购买 nike

我正在尝试编写一个从 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

最佳答案

尝试这些更改:

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循环头实际上是可以的。

关于linux - Shell for 循环,在声明处停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40495962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com