LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-04-2012, 06:49 AM   #1
kverma1985
LQ Newbie
 
Registered: Sep 2012
Posts: 2

Rep: Reputation: Disabled
How to compare string 15.05 with float 15.0 in Linux shell script


I'm using a shell script in Linux environment and comparing values, one is a floating number (15.0) and another is a string containing a floating number (variable)... I'm not able to compare the two. Please help in this...

Code:
#!/bin/sh

value=`command which gives string output ~15.50 or 1.24 or 0.02 etc`

if expr $value \> "15.0" > /dev/null ;
then
more commands follow...
 
Old 09-04-2012, 07:16 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,222

Rep: Reputation: 5445Reputation: 5445Reputation: 5445Reputation: 5445Reputation: 5445Reputation: 5445Reputation: 5445Reputation: 5445Reputation: 5445Reputation: 5445Reputation: 5445
You can try to use bc
Code:
case `echo $var1 - $var2 | bc` in
    -*) 
    ;;
    0)
    ;;
    *)
    ;;
esac
but not tested, just a quick tip
 
1 members found this post helpful.
Old 09-04-2012, 08:12 AM   #3
onebuck
Moderator
 
Registered: Jan 2005
Location: Summer Midwest USA, Central Illinois, Winter Central Florida
Distribution: SlackwareŽ
Posts: 13,652
Blog Entries: 36

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Moderator Response

Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 09-06-2012, 02:30 AM   #4
trey85stang
Senior Member
 
Registered: Sep 2003
Posts: 1,091

Rep: Reputation: 41
ksh93 can handle that if it is available to you. otherwise perl or awk would be a better choice.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to compare float number in shell program olivia dunham Linux - Newbie 1 08-23-2011 03:39 AM
Compare $string from PHP script to /etc/shadow file Karas Linux - Newbie 5 11-27-2009 08:18 AM
Compare first characters of a string in UNIX shell loopoo Linux - Newbie 8 12-11-2006 11:35 AM
count digits of a float || convert float to string nadroj Programming 6 07-11-2005 04:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration