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

标签 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

最佳答案

尝试这些更改:

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/

相关文章:

linux - bash 中的连字符和垂直线

linux - 删除数据库 : could not connect to database template1: could not connect to server

node.js - 如何安全地从 Electron 打开这个外部应用程序?

javascript - Ajax 循环调用

python - 读取 CSV 文件并在没有特定行的情况下重写它们 Python

c - 在 tcsh shell 下,如何将脚本的输出发送到 shell 和 gdb 上的程序?

linux - rpmbuild 已安装(但未打包)的文件源

ios - 无法通过 jenkins shell 脚本使用 xcodebuild 命令创建 IPA

linux - 检查文件名是否与 tar --to-command 中的模式匹配

javascript - 对 XML 文档中的特殊字符进行转义