How to match floating number range in shell scripts?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to match floating number range in shell scripts?
# 1  
Old 08-30-2011
Question How to match floating number range in shell scripts?

for example:

# I want to judge the value of $1 which should be $2<$1<$3.
Code:
temp0=`echo "$1 - $2" | bc`
temp1=`echo "$1 - $3" | bc`

if [[ $temp0 =~ 'how  to match a positive number' && $temp1 =~ 'how  to match a negative number' ]] ; then 
     echo OK
else
     echo False
fi

Moderator's Comments:
Mod Comment Please use code tags, thanks.

Last edited by zaxxon; 08-30-2011 at 05:47 AM.. Reason: code tags and added a "fi"
# 2  
Old 08-30-2011
Try this:
Code:
if [ `echo "$1 > $2" | bc -l` -eq 1 -a `echo "$1 < $3" | bc -l` -eq 1 ]; then
 echo OK
else
 echo False
fi

These 2 Users Gave Thanks to sulti For This Post:
# 3  
Old 08-30-2011
ksh93, bash, zsh:
Code:
if (( $2 < $1 && $1 < $3)); then 
  echo Ok
else
  echo False
done

===

Well, bash doesn't work with floating numbers. Then
Code:
if [[ ! $temp0 =~ ^- && $temp1 =~ ^- ]]; then 
...

These 2 Users Gave Thanks to yazu For This Post:
# 4  
Old 08-30-2011
Above is true, when Your number is in this format:
0,1
It's not working when You have dot in number (like in bc standard):
0.1

Last edited by sulti; 08-30-2011 at 06:33 AM..
This User Gave Thanks to sulti For This Post:
# 5  
Old 08-30-2011
Thank you guys~
The advices all of your give me much help.
# 6  
Old 08-30-2011
In this code
Code:
if [ `echo "$1 > $2" | bc -l` -eq 1 -a `echo "$1 < $3" | bc -l` -eq 1 ]

the meaning of parameter "-a" is "and" operator?
This User Gave Thanks to qcmao For This Post:
# 7  
Old 08-30-2011
Yes ..
Quote:
"-a" --> and
"-o" --> or
These 2 Users Gave Thanks to jayan_jay For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert floating point to a number

Hello Guys, I have a floating point number 1.14475E+15 I want to convert this number in to full number (Integer or Big integer). I tried couple of functions it did not work. When I use INT=${FLOAT/.*} I am getting value as 1. I don't want a truncated value #!/bin/bash #... (9 Replies)
Discussion started by: skatpally
9 Replies

2. UNIX for Dummies Questions & Answers

Bash Floating Number Variables Comparision

I am trying to compare the floating number variables but i am receiving an error, can you please help what is wrong. Thank you. #!/bin/bash var1=100.25 var2=100.25 if (( $var1 == $var2 )); then echo "Matching" else echo "Not Matching" fi Error: ./number.sh: line... (6 Replies)
Discussion started by: Ariean
6 Replies

3. Shell Programming and Scripting

Rounding off to the nearest floating number

I have a number, which I want to convert into the nearest floating number upto two places after the decimal point. E.g. 1.2346 will become 1.23 but 1.2356 will become 1.24 . Similarly 0.009 will be 0.01 and 0.001 will be 0.00 or 0.0 (not 0, wnat to keep the decimal... (1 Reply)
Discussion started by: hbar
1 Replies

4. Shell Programming and Scripting

Grep range of lines to print a line number on match

Hi Guru's, I am trying to grep a range of line numbers (based on match) and then look for another match which starts with a special character '$' and print the line number. I have the below code but it is actually printing the line number counting starting from the first line of the range i am... (15 Replies)
Discussion started by: Kevin Tivoli
15 Replies

5. Shell Programming and Scripting

[BASH] Regex for floating point number

Hey again, I have a basic regex that tests if a number is a float. Thank you. (5 Replies)
Discussion started by: whyte_rhyno
5 Replies

6. Shell Programming and Scripting

Increment a floating number in ksh

Hi ! How to increment a varibale in ksh. #!/bin/ksh set -x RELEASE_NUM=5.2.103 VAL=0.0.1 RELEASE_NUM=`echo $RELEASE_NUM + $VAL | bc` echo $RELEASE_NUM The above code is throwing this error. + RELEASE_NUM=5.2.103 (2 Replies)
Discussion started by: dashok.83
2 Replies

7. Shell Programming and Scripting

problem with floating point number loops

Hey, I guess I am just to stupid and am not seeing the "wood for the trees", but I am always getting strange errors. I want to create a mesh with coordinates like: x y z 3.1 3.0 0.75 0 0 1 3.1 2.9 0.75 0 0 1 3.1 2.8 0.75 0 0 1 3.1 2.7 0.75 0 0 1 3.0 ... (10 Replies)
Discussion started by: ergy1983
10 Replies

8. Shell Programming and Scripting

floating point number problem

Hello folks I Hope everyone is fine. I am calculating number of bytes calculation from apache web log. awk '{ sum += $10 } END { print sum }' /var/httpd/log/mydomain.log 7.45557e+09 it show above number, what should i do it sow number like 7455, i mean if after decimal point above 5 it... (5 Replies)
Discussion started by: learnbash
5 Replies

9. Shell Programming and Scripting

calling 'n' number of shell scripts based on dependency in one shell script.

Hello gurus, I have three korn shell script 3.1, 3.2, 3.3. I would like to call three shell script in one shell script. i m looking for something like this call 3.1; If 3.1 = "complete" then call 3.2; if 3.2 = ''COMPlete" then call 3.3; else exit The... (1 Reply)
Discussion started by: shashi369
1 Replies

10. Shell Programming and Scripting

using bc with floating point number in files

Hi, I' using bash and I would like to use "bc" to compute the ratio of of two numbers and assign the ratio to a variable. The numbers are in a file, e.g. 196.304492 615.348986 Any idea how to do it? N.B. I cannot change the file to have 196.304492 / 615.348986 as the file is produced by... (14 Replies)
Discussion started by: f_o_555
14 Replies
Login or Register to Ask a Question