How to compare two decimal numbers in bash/awk?

前端 未结 7 2154
傲寒
傲寒 2020-12-08 15:09

I am trying to compare two decimal values but I am getting errors. I used

if [ \"$(echo $result1 \'>\' $result2 | bc -l)\" -eq 1 ];then

相关标签:
7条回答
  • 2020-12-08 15:36
    if [[ `echo "$result1 $result2" | awk '{print ($1 > $2)}'` == 1 ]]; then
      echo "$result1 is greater than $result2"
    fi
    
    0 讨论(0)
  • 2020-12-08 15:38

    Why use bc ?

    for i in $(seq -3 0.5 4) ; do echo $i ; if [[ (( "$i" < 2 )) ]] ; then echo "... is < 2";fi; done
    

    The only problem : the comparison "<" doesn't work with negative numbers : they are taken as their absolute value.

    0 讨论(0)
  • 2020-12-08 15:52

    Following up on Dennis's reply:

    Although his reply is correct for decimal points, bash throws (standard_in) 1: syntax error with floating point arithmetic.

    result1=12
    result2=1.27554e-05
    
    
    if (( $(echo "$result1 > $result2" | bc -l) )); then
        echo "r1 > r2"
    else
        echo "r1 < r2"
    fi
    

    This returns incorrect output with a warning although with an exit code of 0.

    (standard_in) 1: syntax error
    r1 < r2

    While there is no clear solution to this (discussion thread 1 and thread 2), I used following partial fix by rounding off floating point results using awk followed by use of bc command as in Dennis's reply and this thread

    Round off to a desired decimal place: Following will get recursive directory space in TB with rounding off at the second decimal place.

    result2=$(du -s "/home/foo/videos" | tail -n1 | awk '{$1=$1/(1024^3); printf "%.2f", $1;}')
    

    You can then use bash arithmetic as above or using [[ ]] enclosure as in following thread.

    if (( $(echo "$result1 > $result2" | bc -l) )); then
        echo "r1 > r2"
    else
        echo "r1 < r2"
    fi
    

    or using -eq operator where bc output of 1 is true and 0 is false

    if [[ $(bc <<< "$result1 < $result2") -eq 1 ]]; then
        echo "r1 < r2"
    else
        echo "r1 > r2"
    fi
    
    0 讨论(0)
  • 2020-12-08 15:53
    if awk 'BEGIN{exit ARGV[1]>ARGV[2]}' "$z" "$y"
    then
      echo z not greater than y
    else
      echo z greater than y
    fi
    
    0 讨论(0)
  • 2020-12-08 15:53

    You can also echo an if...else statement to bc.

    - echo $result1 '>' $result2
    + echo "if (${result1} > ${result2}) 1 else 0"
    
    (
    #export IFS=2  # example why quoting is important
    result1="2.3" 
    result2="1.7" 
    if [ "$(echo $result1 '>' $result2 | bc -l)" -eq 1 ]; then echo yes; else echo no;fi
    if [ "$(echo "if (${result1} > ${result2}) 1 else 0" | bc -l)" -eq 1 ];then echo yes; else echo no; fi
    if echo $result1 $result2 | awk '{exit !( $1 > $2)}'; then echo yes; else echo no; fi
    )
    
    0 讨论(0)
  • 2020-12-08 15:57

    You can do it using Bash's numeric context:

    if (( $(echo "$result1 > $result2" | bc -l) )); then
    

    bc will output 0 or 1 and the (( )) will interpret them as false or true respectively.

    The same thing using AWK:

    if (( $(echo "$result1 $result2" | awk '{print ($1 > $2)}') )); then
    
    0 讨论(0)
提交回复
热议问题