Reputation: 907
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.
Upvotes: 1
Views: 120
Reputation: 203985
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
Upvotes: 0
Reputation: 785491
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
Upvotes: 2