Titus Pullo
Titus Pullo

Reputation: 3821

Decimal for loop shell script

How can I define a for loop in a bash shell script in order to obtain decimal values from 0.50 to 1.00 (i.e 0.50 0.51 etc.) I've found something like this:

for I in $(seq 50 100)
do
    echo $I 10 | awk '{print $1/$2}'    

done

But it simply shows the value, I need to assign it to a variable. How can I do it?

Upvotes: 1

Views: 648

Answers (4)

skatsn
skatsn

Reputation: 1

seq --version
seq (GNU coreutils) 8.4
...

seq 0.5 .01 1.0
0.50
0.51
0.52
...
1.00

Upvotes: 0

Franko
Franko

Reputation: 131

OK here's what I've done

i=$(echo "scale=2;0.50" | bc)

echo $i

for a in {50..100};do
echo $i
i=$(echo "scale=2;$i+0.01" | bc)
done

scale = number of decimals you want ;(operations you want to do) you can / * + - ...

the output of my script is

.50 .50 .51 .52 .53 .54 .55 .56 .57 .58 .59 .60 .61 .62 .63 .64 .65 .66 .67 .68 .69 ... 1.00

Upvotes: 0

choroba
choroba

Reputation: 241848

No need for external commands:

for i in {50..100} ; do
    j=0.$i
    (( i==100 )) && j=1
    echo $j
done

Upvotes: 6

thefourtheye
thefourtheye

Reputation: 239463

for I in $(seq 50 100)
do
    J=`echo $I 10 | awk '{print $1/$2}'`
    echo $J
done

Upvotes: 0

Related Questions