以下if-then-else有什么问题? 我从来没有打过别的。
if [ $result > $value ]; then echo HEALTH: OK. Result is $result Value is $value exit 0 else echo HEALTH: CRITICAL. Result is $result which is over $value exit 2 fi # ./check_cw_auto <AWS variables> 5.0 HEALTH: OK. Result is 275593.8 Value is 5.0 # ./check_cw_auto <AWS variables> 10000000.0 HEALTH: OK. Result is 275593.8 Value is 10000000.0
我正在尝试创build一个nagios bash脚本。 我曾尝试使用-gt而不是>,但是这给了一个错误。 例:
./check_cw_auto <AWS variables> 5.0 ./check_cw_auto: line 30: [: 256497.2: integer expression expected HEALTH: CRITICAL. Result is 256497.2 which is over 5.0
在[] ,两个string按字典顺序进行比较,而不是数字。 你想使用-gt或-lt 。
编辑 :为了响应你的更新, bash没有浮点逻辑。 你需要使用整数。
由于您有一个浮点数作为比较的一部分,您可以使用bc进行比较,如果比较结果为真,则bc返回1
if [ $(echo "$result > $value" | bc -q ) -eq 1 ]; then echo HEALTH: OK. Result is $result Value is $value exit 0 else echo HEALTH: CRITICAL. Result is $result which is over $value exit 2 fi
http://mywiki.wooledge.org/BashPitfalls#A.5B.5B_.24foo_.3E_7_.5D.5D
它被视为string比较。
使用“-gt”代替:
if [ $result -gt $value ]; then