比较bash中的负数 [英] Compare negative numbers in bash
本文介绍了比较bash中的负数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何完成涉及负数的数字比较?
if [[-2> -1]];那么
/ pre>
echo-2> -1
else
echo-2 <= - 1
fi
我也尝试了
-1'];那么
,但条件总是表现为-2大于-1。
当我不使用负数时,比较工作。
我想在纯bash中使用解决方案。 p>
解决方案似乎可以正常运作:
if [[-2-gt-1]];那么
/ pre>
echo-2> -1
else
echo-2 <= - 1
fi
输出:
-2< = - 1
您可能想使用
((...))
,其使得能够根据Shell算术的规则来评估表达式。$((-2 < -1))& echo小于或等于|| echo Larger
小于或等于
$((-2 <= -3))&& echo小于或等于|| echo Larger
Larger
How can i accomplish number comparison involving negative numbers?
if [[ "-2" > "-1" ]]; then echo "-2 >-1" else echo "-2 <=-1" fi
I also tried
if [ '-2' -lt '-1' ]; then
but the condition always behaves as if -2 would be greater than -1.
The comparisons work when i do not use negative numbers.
I would like a solution in pure bash if possible.
解决方案Seems to work correctly:
if [[ "-2" -gt "-1" ]]; then echo "-2 >-1" else echo "-2 <=-1" fi
Output:
-2 <=-1
You might want to use
((...))
which enables the expression to be evaluated according to rules of Shell Arithmetic.$ ((-2 <= -1)) && echo Smaller or equal || echo Larger Smaller or equal $ ((-2 <= -3)) && echo Smaller or equal || echo Larger Larger
这篇关于比较bash中的负数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文