问题
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 thanif [ "$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