bash 使用非整数的 Shell 循环?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2472191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shell loops using non-integers?
提问by mary
I wrote a .sh file to compile and run a few programs for a homework assignment. I have a "for" loop in the script, but it won't work unless I use only integers:
我写了一个 .sh 文件来编译和运行一些家庭作业的程序。我在脚本中有一个“for”循环,但除非我只使用整数,否则它不会工作:
#!/bin/bash
for (( i=10; i<=100000; i+=100))
do
./hw3_2_2 $i
done
The variable $i is an input for the program hw3_2_2, and I have non-integer values I'd like to use. How could I loop through running the code with a list of decimal numbers?
变量 $i 是程序 hw3_2_2 的输入,我想使用非整数值。我怎样才能循环运行带有十进制数字列表的代码?
回答by Daniel C. Sobral
I find it surprising that in five years no one ever mentioned the utility created just for generating ranges, but, then again, it comes from BSD around 2005, and perhaps it wasn't even generally available on Linux at the time the question was made.
我发现令人惊讶的是,五年来没有人提到过仅为生成范围而创建的实用程序,但是,再一次,它来自 2005 年左右的 BSD,也许在提出问题时它甚至还没有在 Linux 上普遍可用.
But here it is:
但这里是:
for i in $(seq 0 0.1 1)
Or, to print all numbers with the same width (by prepending or appending zeroes), use -w. That helps prevent numbers being sent as "integers", if that would cause issues.
或者,要打印具有相同宽度的所有数字(通过添加或添加零),请使用-w. 如果这会导致问题,这有助于防止将数字作为“整数”发送。
The syntax is seq [first [incr]] last, with firstdefaulting to 1, and incrdefaulting to either 1 or -1, depending on whether lastis greater than or less than first. For other parameters, see seq(1).
语法为seq [first [incr]] last,first默认为 1,incr默认为 1 或 -1,具体取决于last是大于还是小于first。对于其他参数,请参阅seq(1)。
回答by unwind
The easiest way is to just list them:
最简单的方法是列出它们:
for a in 1.2 3.4 3.11 402.12 4.2 2342.40
do
./hw3_2_2 $a
done
If the list is huge, so you can't have it as a literal list, consider dumping it in a file and then using something like
如果列表很大,因此您不能将其作为文字列表,请考虑将其转储到文件中,然后使用类似
for a in $(< my-numbers.txt)
do
./hw3_2_2 $a
done
The $(< my-numbers.txt) part is an efficient way (in Bash) to substitute the contents of the names file in that location of the script. Thanks to Dennis Williamson for pointing out that there is no need to use the external catcommand for this.
$(< my-numbers.txt) 部分是一种(在 Bash 中)替换脚本该位置中名称文件内容的有效方法。感谢 Dennis Williamson 指出不需要为此使用外部cat命令。
回答by ghostdog74
you can use awk to generate your decimals eg steps of0.1
您可以使用 awk 生成小数,例如步骤0.1
num=$(awk 'BEGIN{for(i=1;i<=10;i+=0.1)print i}')
for n in $num
do
./hw3_2_2 $n
done
or you can do it entirely in awk
或者你可以完全在 awk 中完成
awk 'BEGIN{cmd="hw3_2_2";for(i=1;i<=10;i+=0.1){c=cmd" "i;system(cmd) } }'
回答by Paused until further notice.
Here's another way. You can use a here doc to include your data in the script:
这是另一种方式。您可以使用此处的文档将您的数据包含在脚本中:
read -r -d '' data <<EOF
1.1
2.12
3.14159
4
5.05
EOF
for i in "$data"
do
./hw3_2_2 "$i"
done
Similarly:
相似地:
array=(
1.1
2.12
3.14159
4
5.05
)
for i in "${array[@]}"
do
./hw3_2_2 "$i"
done
回答by Adrian Tompkins
I usually also use "seq" as per the second answer, but just to give an answer in terms of a precision-robust integer loop and then bc conversion to a float:
我通常也根据第二个答案使用“seq”,但只是为了根据精确稳健的整数循环给出答案,然后 bc 转换为浮点数:
#!/bin/bash
for i in {2..10..2} ; do
x=`echo "scale=2 ; ${i}/10" | bc`
echo $x
done
gives:
给出:
.2 .4 .6 .8 1.0
.2 .4 .6 .8 1.0
回答by Ignacio Vazquez-Abrams
bash doesn't do decimal numbers. Either use something like bcthat can, or move to a more complete programming language. Beware of accuracy problemsthough.
bash 不处理十进制数。要么使用类似的东西bc,要么转向更完整的编程语言。不过要注意精度问题。

