1

I tried to compare floating point number by -gt but it says that point expecting integer value. That means it can not handle floating point number . Then i tried the following code

chi_square=4
if [ "$chi_square>3.84" | bc ]
then
echo yes
else
echo no
fi

But the output is wrong with error . Here is the out put-

line 3: [: missing `]'
File ] is unavailable.
no

Here the no is echoed but it should be yes. I think that's because of the error it's showing. can anybody help me.

1
  • bash cannot handle floating point, but Korn shell (ksh) can. Commented Mar 1, 2016 at 22:20

2 Answers 2

2

If you want to use bc use it like this:

if [[ $(bc -l <<< "$chi_square>3.84") -eq 1 ]]; then
   echo 'yes'
else
   echo 'no'
fi
Sign up to request clarification or add additional context in comments.

Comments

0

Keep it simple, just use awk:

$ awk -v chi_square=4 'BEGIN{print (chi_square > 3.84 ? "yes" : "no")}'
yes

$ awk -v chi_square=3 'BEGIN{print (chi_square > 3.84 ? "yes" : "no")}'
no

or if you prefer avoiding ternary expressions for some reason (and also showing how to use a value stored in a shell variable):

$ chi_square=4
$ awk -v chi_square="$chi_square" 'BEGIN{
    if (chi_square > 3.84) {
        print "yes"
    }
    else {
        print "no"
    }
}'
yes

or:

$ echo "$chi_square" |
awk '{
    if ($0 > 3.84) {
        print "yes"
    }
    else {
        print "no"
    }
}'
yes

or to bring it full circle:

$ echo "$chi_square" | awk '{print ($0 > 3.84 ? "yes" : "no")}'
yes

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.