Shell loops using non-integers?

后端 未结 6 1795
悲哀的现实
悲哀的现实 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:44

    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:

    #!/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

提交回复
热议问题