Bad number error with Linux float variable value comparison operator condition

2

I'm trying to test some logic on an Antminer S9 where I get the output of a command and then from that output I grab a specific float value so I can then put this value into a variable and process with some conditional logic to do some checks and such.

I'm still learning Linux shell and bash scripting so I'm far from an expert so I may be overlooking something simple but I cannot get the variable value of 14167.25 to be checked whether or not it is greater than 1 as a simple example. I think it's because this value is set as a string type and it needs to be an int and I cannot easily figure it out so I thought I'd ask others for help.

Command

cool=$(/usr/bin/bmminer-api stats | grep -o 'GHS 5s].*' | cut -f2 -d'>')

Note: This seems to echo the correct value as expected 14167.25 however I get an error on the conditional logic to compare that number with another.


My trouble starts here I think...

Conditional Command

if [ $cool -gt 1 ]; then echo it is greater than 1; fi

The Error: This gives me an error of sh: 14167.25: bad number.


Other things I tried

I've tried doubling the [[ $cool -gt 1 ]] and also using $($cool) and "$cool" and probably a few other variations for the variable within both double and single square bracket of the if statement and nothing seems to give me the expected result; rookie mistake perhaps!!

I also read over these posts and tried various unsuccessful things I probably shouldn't list:


Other Details

uname -sr
Linux 3.14.0-xilinx-ge8a2f71-dirty

"bmminer-api stats" Command Output

Reply was 'STATUS=S,When=1012053,Code=70,Msg=BMMiner stats,Description=bmminer 1.0.0|BMMiner=2.0.0,Miner=16.8.1.3,CompileTime=Mon Nov 6 17:17:48 CST 2017,Type=Antminer S9|STATS=0,ID=EE50,Elapsed=964517,Calls=0,Wait=0.000000,Max=0.000000,Min=99999999.000000,GHS 5s=14167.25,GHS av=14115.38,miner_count=3,frequency=662,fan_num=2,fan1=0,fan2=0,fan3=5640,fan4=0,fan5=0,fan6=6120,fan7=0,fan8=0,temp_num=3,temp1=0,temp2=0,temp3=0,temp4=0,temp5=0,temp6=64,temp7=61,temp8=65,temp9=0,temp10=0,temp11=0,temp12=0,temp13=0,temp14=0,temp15=0,temp16=0,temp2_1=0,temp2_2=0,temp2_3=0,temp2_4=0,temp2_5=0,temp2_6=74,temp2_7=72,temp2_8=76,temp2_9=0,temp2_10=0,temp2_11=0,temp2_12=0,temp2_13=0,temp2_14=0,temp2_15=0,temp2_16=0,temp3_1=0,temp3_2=0,temp3_3=0,temp3_4=0,temp3_5=0,temp3_6=0,temp3_7=0,temp3_8=0,temp3_9=0,temp3_10=0,temp3_11=0,temp3_12=0,temp3_13=0,temp3_14=0,temp3_15=0,temp3_16=0,freq_avg1=0.00,freq_avg2=0.00,freq_avg3=0.00,freq_avg4=0.00,freq_avg5=0.00,freq_avg6=656.22,freq_avg7=650.38,freq_avg8=656.88,freq_avg9=0.00,freq_avg10=0.00,freq_avg11=0.00,freq_avg12=0.00,freq_avg13=0.00,freq_avg14=0.00,freq_avg15=0.00,freq_avg16=0.00,total_rateideal=14001.40,total_freqavg=654.49,total_acn=189,total_rate=14167.24,chain_rateideal1=0.00,chain_rateideal2=0.00,chain_rateideal3=0.00,chain_rateideal4=0.00,chain_rateideal5=0.00,chain_rateideal6=4666.72,chain_rateideal7=4667.13,chain_rateideal8=4667.54,chain_rateideal9=0.00,chain_rateideal10=0.00,chain_rateideal11=0.00,chain_rateideal12=0.00,chain_rateideal13=0.00,chain_rateideal14=0.00,chain_rateideal15=0.00,chain_rateideal16=0.00,temp_max=65,Device Hardware%=0.0000,no_matching_work=502,chain_acn1=0,chain_acn2=0,chain_acn3=0,chain_acn4=0,chain_acn5=0,chain_acn6=63,chain_acn7=63,chain_acn8=63,chain_acn9=0,chain_acn10=0,chain_acn11=0,chain_acn12=0,chain_acn13=0,chain_acn14=0,chain_acn15=0,chain_acn16=0,chain_acs1=,chain_acs2=,chain_acs3=,chain_acs4=,chain_acs5=,chain_acs6= oooooooo oooooooo oooooooo oooooooo oooooooo oooooooo oooooooo ooooooo,chain_acs7= oooooooo oooooooo oooooooo oooooooo oooooooo oooooooo oooooooo ooooooo,chain_acs8= oooooooo oooooooo oooooooo oooooooo oooooooo oooooooo oooooooo ooooooo,chain_acs9=,chain_acs10=,chain_acs11=,chain_acs12=,chain_acs13=,chain_acs14=,chain_acs15=,chain_acs16=,chain_hw1=0,chain_hw2=0,chain_hw3=0,chain_hw4=0,chain_hw5=0,chain_hw6=192,chain_hw7=152,chain_hw8=155,chain_hw9=0,chain_hw10=0,chain_hw11=0,chain_hw12=0,chain_hw13=0,chain_hw14=0,chain_hw15=0,chain_hw16=0,chain_rate1=,chain_rate2=,chain_rate3=,chain_rate4=,chain_rate5=,chain_rate6=4750.90,chain_rate7=4729.46,chain_rate8=4686.88,chain_rate9=,chain_rate10=,chain_rate11=,chain_rate12=,chain_rate13=,chain_rate14=,chain_rate15=,chain_rate16=,chain_xtime6={X15=1,X37=1,X48=4,X56=1,X57=1},chain_xtime7={},chain_xtime8={X16=4,X38=11},chain_offside_6=0,chain_offside_7=0,chain_offside_8=0,chain_opencore_6=1,chain_opencore_7=1,chain_opencore_8=0,miner_version=16.8.1.3,miner_id=999c7401123b999c|'
[STATUS] =>
(
   [STATUS] => S
   [When] => 1012053
   [Code] => 70
   [Msg] => BMMiner stats
   [Description] => bmminer 1.0.0
)v
[BMMiner2.0.0] =>
(
   [BMMiner] => 2.0.0
   [Miner] => 16.8.1.3
   [CompileTime] => Mon Nov 6 17:17:48 CST 2017
   [Type] => Antminer S9
)
[STATS0] =>
(
   [STATS] => 0
   [ID] => EE50
   [Elapsed] => 964517
   [Calls] => 0
   [Wait] => 0.000000
   [Max] => 0.000000
   [Min] => 99999999.000000
   [GHS 5s] => 14167.25
   [GHS av] => 14115.38
   [miner_count] => 3
   [frequency] => 662
   [fan_num] => 2
   [fan1] => 0
   [fan2] => 0
   [fan3] => 5640
   [fan4] => 0
   [fan5] => 0
   [fan6] => 6120
   [fan7] => 0
   [fan8] => 0
   [temp_num] => 3
   [temp1] => 0
   [temp2] => 0
   [temp3] => 0
   [temp4] => 0
   [temp5] => 0
   [temp6] => 64
   [temp7] => 61
   [temp8] => 65
   [temp9] => 0
   [temp10] => 0
   [temp11] => 0
   [temp12] => 0
   [temp13] => 0
   [temp14] => 0
   [temp15] => 0
   [temp16] => 0
   [temp2_1] => 0
   [temp2_2] => 0
   [temp2_3] => 0
   [temp2_4] => 0
   [temp2_5] => 0
   [temp2_6] => 74
   [temp2_7] => 72
   [temp2_8] => 76
   [temp2_9] => 0
   [temp2_10] => 0
   [temp2_11] => 0
   [temp2_12] => 0
   [temp2_13] => 0
   [temp2_14] => 0
   [temp2_15] => 0
   [temp2_16] => 0
   [temp3_1] => 0
   [temp3_2] => 0
   [temp3_3] => 0
   [temp3_4] => 0
   [temp3_5] => 0
   [temp3_6] => 0
   [temp3_7] => 0
   [temp3_8] => 0
   [temp3_9] => 0
   [temp3_10] => 0
   [temp3_11] => 0
   [temp3_12] => 0
   [temp3_13] => 0
   [temp3_14] => 0
   [temp3_15] => 0
   [temp3_16] => 0
   [freq_avg1] => 0.00
   [freq_avg2] => 0.00
   [freq_avg3] => 0.00
   [freq_avg4] => 0.00
   [freq_avg5] => 0.00
   [freq_avg6] => 656.22
   [freq_avg7] => 650.38
   [freq_avg8] => 656.88
   [freq_avg9] => 0.00
   [freq_avg10] => 0.00
   [freq_avg11] => 0.00
   [freq_avg12] => 0.00
   [freq_avg13] => 0.00
   [freq_avg14] => 0.00
   [freq_avg15] => 0.00
   [freq_avg16] => 0.00
   [total_rateideal] => 14001.40
   [total_freqavg] => 654.49
   [total_acn] => 189
   [total_rate] => 14167.24
   [chain_rateideal1] => 0.00
   [chain_rateideal2] => 0.00
   [chain_rateideal3] => 0.00
   [chain_rateideal4] => 0.00
   [chain_rateideal5] => 0.00
   [chain_rateideal6] => 4666.72
   [chain_rateideal7] => 4667.13
   [chain_rateideal8] => 4667.54
   [chain_rateideal9] => 0.00
   [chain_rateideal10] => 0.00
   [chain_rateideal11] => 0.00
   [chain_rateideal12] => 0.00
   [chain_rateideal13] => 0.00
   [chain_rateideal14] => 0.00
   [chain_rateideal15] => 0.00
   [chain_rateideal16] => 0.00
   [temp_max] => 65
   [Device Hardware%] => 0.0000
   [no_matching_work] => 502
   [chain_acn1] => 0
   [chain_acn2] => 0
   [chain_acn3] => 0
   [chain_acn4] => 0
   [chain_acn5] => 0
   [chain_acn6] => 63
   [chain_acn7] => 63
   [chain_acn8] => 63
   [chain_acn9] => 0
   [chain_acn10] => 0
   [chain_acn11] => 0
   [chain_acn12] => 0
   [chain_acn13] => 0
   [chain_acn14] => 0
   [chain_acn15] => 0
   [chain_acn16] => 0
   [chain_acs1] =>
   [chain_acs2] =>
   [chain_acs3] =>
   [chain_acs4] =>
   [chain_acs5] =>
   [chain_acs6] =>  oooooooo oooooooo oooooooo oooooooo oooooooo oooooooo oooooooo ooooooo
   [chain_acs7] =>  oooooooo oooooooo oooooooo oooooooo oooooooo oooooooo oooooooo ooooooo
   [chain_acs8] =>  oooooooo oooooooo oooooooo oooooooo oooooooo oooooooo oooooooo ooooooo
   [chain_acs9] =>
   [chain_acs10] =>
   [chain_acs11] =>
   [chain_acs12] =>
   [chain_acs13] =>
   [chain_acs14] =>
   [chain_acs15] =>
   [chain_acs16] =>
   [chain_hw1] => 0
   [chain_hw2] => 0
   [chain_hw3] => 0
   [chain_hw4] => 0
   [chain_hw5] => 0
   [chain_hw6] => 192
   [chain_hw7] => 152
   [chain_hw8] => 155
   [chain_hw9] => 0
   [chain_hw10] => 0
   [chain_hw11] => 0
   [chain_hw12] => 0
   [chain_hw13] => 0
   [chain_hw14] => 0
   [chain_hw15] => 0
   [chain_hw16] => 0
   [chain_rate1] =>
   [chain_rate2] =>
   [chain_rate3] =>
   [chain_rate4] =>
   [chain_rate5] =>
   [chain_rate6] => 4750.90
   [chain_rate7] => 4729.46
   [chain_rate8] => 4686.88
   [chain_rate9] =>
   [chain_rate10] =>
   [chain_rate11] =>
   [chain_rate12] =>
   [chain_rate13] =>
   [chain_rate14] =>
   [chain_rate15] =>
   [chain_rate16] =>
   [chain_xtime6] => {X15=1
   [X37] => 1
   [X48] => 4
   [X56] => 1
   [X57] => 1}
   [chain_xtime7] => {}
   [chain_xtime8] => {X16=4
   [X38] => 11}
   [chain_offside_6] => 0
   [chain_offside_7] => 0
   [chain_offside_8] => 0
   [chain_opencore_6] => 1
   [chain_opencore_7] => 1
   [chain_opencore_8] => 0
   [miner_version] => 16.8.1.3
   [miner_id] => 999c7401123b999c
)

Pimp Juice IT

Posted 2018-11-05T00:57:04.647

Reputation: 29 425

Answers

3

As specified in the "How can I calculate with floating point numbers instead of just integers?" post, the main point is that "BASH's builtin arithmetic uses integers only" and you are working with a float data type which BASH cannot perform comparison operations against.

Workaround

As a workaround such as talked about in the "Floating Point Comparison in Shell Script" post, you can use bc to perform the comparison operations of the float data types. You can evaluation it's result of true or false and have the conditional logic process accordingly based on its result.

Example Logic

if [ "$(echo "$cool > 1" | bc)" -eq 1 ]
then
    echo "${cool} is greater than 1"
fi

l0b0

Posted 2018-11-05T00:57:04.647

Reputation: 6 306

This seems to be what I needed and the thing I was totally overlooking. Of course I'm using a float data type for this operation. This got me going in the right direction for sure. – Pimp Juice IT – 2018-11-05T02:10:09.840

Is using the [ "$(echo "$cool > 1" | bc)" -eq 1 ] over the [ $(bc <<<"$cool > 1") -eq 1 ] syntax purely personal preference that you know of or does the other way have some sort of gotcha or something? – Pimp Juice IT – 2018-11-05T02:20:47.443

2<<< is a bashism; my change makes it POSIX compliant. The question mentions Bash but not the tags, so I thought it was better to be on the safe side. – l0b0 – 2018-11-05T02:30:49.107