java - Dependent Nested For Loops in Bash
问题描述
I am trying to implement a nested for loop in bash where the inner loop uses the current value of the outer loop in its range but I am getting this error "/drawgraph.sh: line 19: {0..1}: syntax error: operand expected (error token is "{0..1}")"
Here is my code:
for i in {0..499}
do
for j in {0..$i}
do
# other code
done
done
Here's a java analogy for what I'm trying to do:
for (int i = 0; i < 499; i++) {
for (int j = 0; j < i; j++) {
// some code
}
}
解决方案
您可以只使用((i=0;i<499;i++))
而不是{0..499}
:
#!/bin/bash
for ((i=0;i<499;i++))
do
for ((j=0;j<i;j++))
do
echo "$i $j"
done
done
如果你想使用数组语法,你应该使用$(seq 0 $i)
而不是{0..$i}
:
#!/bin/bash
for i in {0..499}
do
for j in $(seq 0 $i)
do
echo "$i $j"
done
done
推荐阅读
- react-native - RN expo 独立应用程序在 LAN 环境中使用 Authsession,可能吗?
- python - 匹配数据框之间的行并保持顺序
- javascript - 显示隐藏的 div 不是下推其他内容,而是在页面的另一部分
- mysql - 是否可以要求 ProxySQL 在读取时使用特定的 GTID?
- python - arg_parse 无或列出熊猫 read_table
- c# - 使用正则表达式提取组并在一行中构造 URL
- javascript - 如何展平对象内部的数组?
- android - 如何使用 CodeceptJS 在 Appium 中识别带有 id 的字段?
- sql - 我可以在 SQL Server 上创建动态表值函数吗?
- c# - 没有这样的文件或目录,在 .net core 中运行控制台应用程序时无法在 docker 容器中找到 Python.exe