How to compare a number stored as string to an integer value in shell scripting? Here is what I have,

if [$temp -ge 2.5] then;

when I run the above code, it gives the error as "Integer Expression expected".
Here the temp variable contains the number "3.0" as a string, I want to compare the value of temp is greater or equal to the number 2.5.
How can I do it.

Please reply soon.

Thanks in advance.

Shiva

Recommended Answers

All 4 Replies

an interger is a whole number, with no decimal point. That is why it is telling you an integer is expected.

either the number 2.5 or 3.0 will cause it to fail.

Does any one have the pseudocode for this problem?? If you do plaz contact me

Thanx

Here's some code for C# that works:

You can use doubles to hold numbers with decimal places.

if [$temp.convert.todouble() -ge 2.5] then;

that should solve your problem.

Try putting quotes around 2.5 and $temp like so:

if ["$temp" -ge "2.5"] then;

otherwise set a variable equal to 2.5 and test that:

SOME_VALUE=2.5
if [$temp -ge $SOME_VAULE] then;

This may require quotes too so...

These suggestions are based on BASH shell scripting which is what this kind of looks like, so your mileage may vary...

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.