bash中的条件语句 [英] Conditional statement in bash
本文介绍了bash中的条件语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
x=23; y=223; [[ $x < $y ]] &&
echo yes || echo no;
有人可以向我解释为什么我得到的答案是否"而不是是".我是bash的新手.如果有人可以解释这一点,我将不胜感激.
Can someone explain me why am i getting answer as no instead of yes. I am new to bash. I would appreciate if someone could explain this.
推荐答案
使用 -lt
进行数值比较:
x=23; y=223; [[ $x -lt $y ]] && echo yes || echo no;
yes
<
或>
或 ==
运算符进行字符串比较,其中 23
在*字典上*磨碎了 223
,因此没有提供
输出.
<
or >
or ==
operators do string comparison where 23
is * lexicographically* not grater that 223
hence giving no
output.
或更佳的BASH中的((...))
算术评估器:
Or better ((...))
arithmetic evaluator in BASH:
x=23; y=223; (( x < y )) && echo yes || echo no
yes
这篇关于bash中的条件语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文