Linux 浮点变量值比较运算符条件下的错误数字错误

Linux 浮点变量值比较运算符条件下的错误数字错误

我正在尝试在 Antminer S9 上测试一些逻辑,我得到一个命令的输出,然后从该输出中获取一个特定的浮点值,这样我就可以将这个值放入变量中并使用一些条件逻辑进行处理以进行一些检查等。

我仍在学习 Linux shell 和 bash 脚本,因此我远非专家,所以我可能会忽略一些简单的事情,但我无法获取要14167.25检查的变量值是否大于1作为一个简单的示例。我认为这是因为这个值设置为字符串类型,并且它需要是一个int,我无法轻易弄清楚,所以我想向其他人寻求帮助。

命令

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

笔记:这似乎符合预期,呼应了正确的值14167.25,但是在将该数字与另一个数字进行比较的条件逻辑上出现错误。


我想我的麻烦就从这里开始了......

条件命令

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

错误:这给了我一个错误sh: 14167.25: bad number


我尝试过的其他事情

我尝试过将 and 加倍[[ $cool -gt 1 ]],也尝试过使用$($cool)and"$cool"以及可能还有一些其他的变体,在 if 语句的双方括号和单方括号内对变量进行处理,但似乎没有什么能给我预期的结果;也许是新手错误!!

我也阅读了这些帖子并尝试了各种我可能不应该列出的不成功的事情:


其他详情

uname -sr
Linux 3.14.0-xilinx-ge8a2f71-dirty

bmminer-api stats”命令输出

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
)

答案1

“我怎样才能用浮点数而不是整数来计算?”帖子的重点是“BASH 的内置算法仅使用整数”你正在使用 BASH 无法执行的浮点数据类型比较运算反对。

解决方法

作为一种解决方法,例如在“Shell 脚本中的浮点比较”发布后,你可以使用bc执行浮点数据类型的比较运算。您可以评估其结果为真或假,并根据其结果进行相应的条件逻辑处理。

示例逻辑

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

相关内容