Linux 比较变量,而不考虑bash中的标识
我想比较Linux 比较变量,而不考虑bash中的标识,linux,bash,Linux,Bash,我想比较bash中的两个变量,看看变量是否超过了截止值 比如说 截止值=500.00 cut_off=500.00 现在,我想检查一个变量,不管它的标志是否超过截止值值 source_1=-510.00 target_1=1620.00 source_2=490.00 target_2=-488.13 source_3=5490.00 target_3=-4488.13 现在,当要检查的变量大于cut\u off时,则echo表示变量值随要检查的cut\u off变量的差异而变化` 我已
bash
中的两个变量,看看变量是否超过了截止值
比如说
截止值=500.00
cut_off=500.00
现在,我想检查一个变量,不管它的标志是否超过截止值
值
source_1=-510.00
target_1=1620.00
source_2=490.00
target_2=-488.13
source_3=5490.00
target_3=-4488.13
现在,当要检查的变量大于cut\u off
时,则echo
表示变量值随要检查的cut\u off变量的差异而变化`
我已经像下面这样做了,但它没有打印正确的错误声明
cutoff=500.0
actual_diff=$(echo "${source}" - "${target}" | bc)
if (( $(echo "$actual_diff > $cutoff" |bc -l) ));
then
echo -e "Difference between source and target is more than cut_off Actual difference is ${actual_diff}"
exit 1
else
echo -e "Difference between source and target is in cut_off range Actual difference is ${actual_diff}"
fi
好的,这是我的建议
cutoff=500.0
actual_diff=$(echo "${source}" - "${target}" | bc)
if (( $(echo "${actual_diff#-} > ${cutoff#-}" |bc -l) ));
then
echo -e "Difference between source and target is more than cut_off Actual difference is ${actual_diff}"
exit 1
else
echo -e "Difference between source and target is in cut_off range Actual difference is ${actual_diff}"
fi
(感谢以下帖子)什么是截止范围?0到截止?@kalou.net它介于-500.00到500.00之间
因此,如果我理解正确,您希望将实际_diff的“绝对值”与截止的“绝对值”进行比较。@kalou.net是的,正确