naruto uzumaki
naruto uzumaki

Reputation: 39

How to compare floating value in shell script?

I am a beginner in shell scripting. I Have a decimal value and I want to compare it in a if statement. I was trying like this

size1=`${PATH_TO_CLIENT}sqlplus $IMPUSER/$IMPPWD@$ENDPOINT<< EOF | sed -n '/^\s*SIZE_GB$/{n;n;n;p}'
select owner, sum(bytes)/1024/1024/1024 Size_GB from dba_segments where owner = 'XXXXXX' group by owner;
exit;
EOF`
echo "Total data is = ${size1}"

if (($size1 > 7.50 | bc))
then
  echo '### THE DATA SIZE IS GREATER THAN 7.5 GB ###'
  echo 'Total data is ${size1}'
  exit 1
else
  echo 'Total data is {$size1}'
  echo '### THE DATA SIZE IS OKAY ###'
fi

The output I am getting is

Total data is = 11.2345
./testDelete.sh: line 65: ((: 11.2345 > 7.50 | bc: syntax error: invalid arithmetic operator (error token is ".2345 > 7.50 | bc")
Total data is {$size1}
### THE DATA SIZE IS OKAY ###

So size1 variable is able to store the value but I was not able to compare it. Please can anyone help me with it.

Upvotes: 0

Views: 182

Answers (4)

Darkman
Darkman

Reputation: 2981

Pure shell solution

$ if [ 7 \> 7.5 ]; then echo 'Y'; else echo 'N'; fi
N
$ if [ 7.4 \> 7.5 ]; then echo 'Y'; else echo 'N'; fi
N
$ if [ 7.5 \> 7 ]; then echo 'Y'; else echo 'N'; fi
Y
$ if [ 7.5 \> 7.4 ]; then echo 'Y'; else echo 'N'; fi
Y
$ if [ 7 \< 7.5 ]; then echo 'Y'; else echo 'N'; fi
Y
$ if [ 7.4 \< 7.5 ]; then echo 'Y'; else echo 'N'; fi
Y
$ if [ 7.5 \< 7 ]; then echo 'Y'; else echo 'N'; fi
N
$ if [ 7.5 \< 7.4 ]; then echo 'Y'; else echo 'N'; fi
N
$ if [ 7 = 7.5 ]; then echo 'Y'; else echo 'N'; fi
N
$ if [ 7.4 = 7.5 ]; then echo 'Y'; else echo 'N'; fi
N
$ if [ 7.5 = 7.5 ]; then echo 'Y'; else echo 'N'; fi
Y

Upvotes: 1

KamilCuk
KamilCuk

Reputation: 141493

I like to do small unsafe awk wrapper:

float_cmp() {
    awk "BEGIN{exit(!( $* ))}" <&-
}

if float_cmp "$size1 > 7.50"; then

Upvotes: 2

Luuk
Luuk

Reputation: 14948

you could change: if (($size1 > 7.50 | bc)) to:

if (( $(echo "$size1 > 7.50" | bc) ))

The $(...) makes the command echo "$size1 > 7.50" | bc being executed. This produces 0 or 1.

Upvotes: 2

nino
nino

Reputation: 678

A while ago, I had the same problem. I did some searches and ended up with this function:

function compare_floats(){
    ## https://stackoverflow.com/questions/8654051/how-to-compare-two-floating-point-numbers-in-bash
    ## usage: is_smaller="$(compare_floats "$num1" "<" "$num2")"  ## returns true or false

    local float1="$1"
    local oper="$2"
    local float2="$3"

    ## compare
    case "$oper" in
         "<" ) [ $(awk '{printf($1 <  $2) ? 1 : 0}' <<< "$float1 $float2") -eq 1 ] && local result="true" || local result="false" ;;
         "=" ) [ $(awk '{printf($1 == $2) ? 1 : 0}' <<< "$float1 $float2") -eq 1 ] && local result="true" || local result="false" ;;
         ">" ) [ $(awk '{printf($1 >  $2) ? 1 : 0}' <<< "$float1 $float2") -eq 1 ] && local result="true" || local result="false" ;;
    esac

    echo "$result" ;}


condition="$(compare_floats 41.4 "<" 19.2)"
[ "$condition" == "true" ] && <do_this> || <do_that>

Upvotes: 1

Related Questions