What is “-le” in shell script?

走远了吗. 提交于 2019-12-18 09:13:30

问题


I am going through this code. I would like to know what is meant by -le in the following code segment.

if [ $stage -le 2 ]; then

In one of the questions it says that -le stands for <= of strings, but that is in Perl. Is it the same here as well?

Further, I would like to know if that $stage variable automatically gets updated. It has been initialized to 0 at the beginning, but later, how does that get incremented?


回答1:


-le checks if the value of left operand is less than or equal to (<=) the value of right operand, if yes then condition becomes true.

consider:

$a=10
$b=20

then [ $a -le $b ] is true.




回答2:


As is stated in the documentation:

integer comparison

(...)

-lt: is less than

if [ "$a" -lt "$b" ]

So it interprets the values of $a and $b (in your case $stage and 2) as integers and performs a comparison. If the first element is less than or equal to the second, the test succeeds and the then part will be executed.

As the documentation later states, one can use <= as well:

<=: is less than or equal to (within double parentheses)

(("$a" <= "$b"))

But then one uses double parentheses (as specified in the documentation).




回答3:


-le is less than or equals to :

if [ $stage -le 2 ];

is same as:

stage <= 2


来源:https://stackoverflow.com/questions/34807689/what-is-le-in-shell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!