Search code examples
bashfilenames

Truncated float number as variable filename in bash


I wanted to do a for loop for all files with name "r?.???.txt", where ?.??? was a float from 0.000 to 3.999. However, bash does not have integer, as far as I know... That turns into a headache...

I tried to do for tedious for loop with two variables representing the numbers before and after decimal. However, I still need to insert zeros if you have ?.00?. Therefore I will need a proper treatment of float numbers and how to store / output them.

In summary, there are two questions I face

  1. doing float in bash
  2. output that float as a value and pass into filename

Thank you.


Solution

  • Using 2 variables is a good idea.

    You can keep the zero padding format with seq -f option :

    #!/bin/bash
    declare -i int=3;
    declare -i dec=1000;
    
    for i in $(seq 1 $int);do
            for j in $(seq -f "%03g" 1 $dec);
                    do echo r$i.$j.txt;  # or do find . -name "r$i.$j.txt";
            done;
    done;