User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Legacy and Other Languages section within the Software Development category of DaniWeb, a massive community of 392,008 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,222 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Legacy and Other Languages advertiser:
Views: 67895 | Replies: 4
Reply
Join Date: Jun 2005
Posts: 1
Reputation: justsomerat is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
justsomerat justsomerat is offline Offline
Newbie Poster

to convert string to integer value

  #1  
Jun 15th, 2005
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: to convert string to integer value

  #2  
Jun 16th, 2005
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.
Reply With Quote  
Join Date: Mar 2006
Posts: 2
Reputation: Cosla is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Cosla Cosla is offline Offline
Newbie Poster

Re: to convert string to integer value

  #3  
Mar 17th, 2006
Does any one have the pseudocode for this problem?? If you do plaz contact me

Thanx
Reply With Quote  
Join Date: Aug 2005
Posts: 27
Reputation: vartotojas is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
vartotojas vartotojas is offline Offline
Light Poster

Re: to convert string to integer value

  #4  
Jun 9th, 2006
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.
Last edited by vartotojas : Jun 9th, 2006 at 10:56 am.
Reply With Quote  
Join Date: Aug 2005
Location: North of the equator, but south of anywhere it could get cold for long
Posts: 25
Reputation: mittelgeek is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
mittelgeek's Avatar
mittelgeek mittelgeek is offline Offline
Light Poster

Re: to convert string to integer value

  #5  
May 30th, 2007
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...
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Legacy and Other Languages Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Legacy and Other Languages Forum

All times are GMT -4. The time now is 10:24 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC