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.

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

Try this:

if [ `echo "$1 > $2" | bc -l` -eq 1 -a `echo "$1 < $3" | bc -l` -eq 1 ]; then
 echo OK
else
 echo False
fi
2 Likes

ksh93, bash, zsh:

if (( $2 < $1 && $1 < $3)); then 
  echo Ok
else
  echo False
done

===

Well, bash doesn't work with floating numbers. Then

if [[ ! $temp0 =~ ^- && $temp1 =~ ^- ]]; then 
...
2 Likes

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

1 Like

Thank you guys~
The advices all of your give me much help.

In this 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?

1 Like

Yes ..

2 Likes