Shell loops using non-integers?

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

    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 cat command for this.

提交回复
热议问题