0

Hi all

I hav written a very small shell script in bin bash & its not working. The script is ::

#!/bin/bash
i=1.0
while [ $i -le 3.0 ]
do
	i=`expr "$i + 0.5" | bc`;
	 echo "i=$i"
done

The error is :

line 6: [: 1.0: integer expression expected

I think the use of decimals in the while loop is creating problems.

Please help, in urgent need.

Regards
Alice

Edited by alice06: n/a

3
Contributors
3
Replies
5
Views
6 Years
Discussion Span
Last Post by alice06
0

You're right, the problem is that test only does integer comparisons.

How about

#!/bin/bash
i=10
while [ $i -le 30 ]
do
        i=`echo "$i + 5" | bc`
        j=$(echo "scale=1; $i / 10" | bc)
        echo "i=$i  j=$j"
done

and use j instead

0

Your problem is comming from the test where it is comparing [1.0 -le 3.0]

Instead of looping from 1.0 to 3.0 how about using a for loop like this:

for x in 1.0 1.5 2.0 2.5 3.0
do
echo $x
done

0

Hey Shibblez,rch1231 Thanx a ton for replying.

Shibblez, using the script, I was able to get my job done perfectly.

Thanx for helping out, thank u very much.

Regards
Alice

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.