Shell loops using non-integers?

后端 未结 6 1785
悲哀的现实
悲哀的现实 2021-02-01 22:05

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:



        
6条回答
  •  不思量自难忘°
    2021-02-01 22:56

    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.

    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.

    The syntax is seq [first [incr]] last, with first defaulting to 1, and incr defaulting to either 1 or -1, depending on whether last is greater than or less than first. For other parameters, see seq(1).

提交回复
热议问题