LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 02-27-2012, 01:42 PM   #1
muebi
LQ Newbie
 
Registered: Feb 2012
Posts: 1

Rep: Reputation: Disabled
bash: how to compare REAL numeric values?


Folks,

I would like to do an if conditional in bash to compare a numeric value, which is NOT an integer:

if [ "$VALUE" -eq 0.1 ]; then
... do something ...
else
... do something else ...
fi

If $VALUE is a real variable (e.g., 0.5) and not an integer and I get an error message saying: $VALUE: integer expression expected.

How can I do this?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 02-27-2012, 01:49 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,631

Rep: Reputation: 519Reputation: 519Reputation: 519Reputation: 519Reputation: 519Reputation: 519
Bash does not handle floating point numbers. Use a real language (perl, python, C, etc) or a more modern shell (zsh) or insert calls to bc.

Last edited by AlucardZero; 02-27-2012 at 01:52 PM.
 
Old 02-27-2012, 01:54 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Native bash: you can't

Two options:
1. move your script to another language with real number support (e.g. python or perl). If you are dealing with floating point numbers, the task (in my experience) is better suited to one of those other languages than a shell script.
2. invoke an external command that can support real valued conditionals

For #2, I usually rely on the bc command.

For instance:
Code:
user@localhost$ echo " 3.5 < 9.1" | bc -l
1
user@localhost$ echo " 3.5 > 9.1" | bc -l
0
Use the above with command substitution. For example:
Code:
variableName=$( echo "${operand1} < ${operand2}" | bc -l )
And then test the value of variableName.

EDIT:
Alucard beat me to it!
 
2 members found this post helpful.
Old 02-28-2012, 08:00 AM   #4
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
You can also do it in awk if bc is not available.

Quote:
Originally Posted by AlucardZero View Post
Bash does not handle floating point numbers. Use a real language (perl, python, C, etc) or a more modern shell (zsh) or insert calls to bc.
So bash isn't a "real" language ?
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH scripting: check for numeric values linuxLuser Programming 11 11-14-2011 10:11 AM
[SOLVED] C Arguments to main - can't compare values madsovenielsen Programming 2 07-25-2011 07:17 AM
Bash: Insert output of a commando into an array and compare the values tengblad Programming 2 04-07-2009 03:36 PM
Compare Decimal Values rbautch Linux - General 10 04-22-2008 04:11 AM
Searching for numeric values in PHP TheBeli Programming 5 04-17-2008 03:55 AM


All times are GMT -5. The time now is 07:28 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration