Guillermo Mosse
Guillermo Mosse

Reputation: 462

Loop in bash from 0.01 to 0.5

I am creating a script in bash to call a command 100 times, varying a float parameter each time. I need to call it with 0.01, then 0.02, 0.03 up to 0.5. As I cannot create a loop using a float in the condition or in the step I am not sure of how to do it. I tried doing the loop from 1 to 50 and then inside dividing the number by 100, transforming that into a float, and calling the command with that, but then the command (basically I am calling Weka and the number corresponds to the confidence interval) doesn't recognize the parameter correctly so maybe I am using the wrong datatype.

Thanks in advance! Patricio

Upvotes: 0

Views: 2042

Answers (4)

DomTomCat
DomTomCat

Reputation: 8569

choose one of the answers, but if you want to use real floating values (instead of strings), then consider using a locale change, if your outside of the floating precision "dot" zone:

seq 0.1 0.5 1.1
0,1
0,6
1,1

whereas with locale

LANG=en;seq 0.1 0.5 1.1
0.1
0.6
1.1 

Upvotes: 0

Dimitris Mintis
Dimitris Mintis

Reputation: 103

for i in $(seq 0.01 0.01 0.5);do
...
done

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200273

If you really cannot use the solution proposed by abasu (+1) you could try something like this:

for i in `seq 1 50`; do
  n=$(echo $i | awk '{printf "%.2f", $1 / 100}')
  echo $n
done

or like this:

for i in `seq 1 50`; do
  n=$(printf "0.%02d" $i)
  echo $n
done

Upvotes: 0

abasu
abasu

Reputation: 2524

for i in `seq 0.01 0.01 0.5`; do  echo "called with $i" ; done

this will create a seq of 0.01 to 0.5 in increment of 0.01. So replace the echo with your needed call.

Upvotes: 8

Related Questions