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

Recommended Answers

All 3 Replies

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.