Shell programming condition test

  
 

First, conditional test syntax:

  • test test content
  • [test content] ##Note there is a space between the brackets and the test content
  • [[ ,null,null,3],Test content]]
  • ((test content)) ## can only be used to test the value of the value, detailed later

    The first three test syntax is basically the same.

    Second, numerical test:

    2.1 Common numerical test:

  • int1 -eq int2 If int1 is equal to int2, return true
  • Int1 -ne int2 Returns true if int1 is not equal to int2
  • int1 -lt int2 Returns true if int1 is less than int2
  • int1 -gt int2 Returns true if int1 is greater than int2 Br>
  • int1 -le int2 Returns true if int1 is less than or equal to int2
  • int1 -ge int2 Returns true if int1 is greater than or equal to int2

    2.2 )) test


  • & lt; smaller than (use) ((& quot in double parentheses; int1 " & lt; " int2 ")) is equivalent to [int1 -lt int2]


    [root@server1 ~]# (( "2" < "3" ))[root@server1 ~]# echo $?0[root@server1 ~]# [ ,null,null,3],1 -lt 3 ][root@server1 ~]# echo $?0

  • <= Less than or equal to (used in double brackets) (("int1" <= "int2" )) is equivalent to [ int1 -ne int2 ]
  • > is greater than (in double brackets) (("int1" > "int2")) Same as [ int1 -gt int2 ]
  • >= greater than or equal to (used in double brackets) (("int1" >= "int2")) is equivalent to [ int1 -ge int2 ]< Br>

    Note: Double parenthesis test can't test the case of equal value. If you use double parenthesis test, it will report the following error:

    [root@server1 ~]# (( "2" = "3" ))-bash: ((: 2 = 3 : attempted assignment to non-variable (error token is "= 3 ")

    3, test string:
    < Br>

  • -z string String is an empty string (length 0) Returns true
  • -n string String returns true if it is a non-empty string
  • str1 = str2 Returns true if string str1 and string str2 are equal
  • str1 == str2 same=
  • str1 != str2 Returns true if string str1 and string str2 are not equal
  • str1 < str2 sorted in lexicographic order, the string str1 is not used before the string str2,
  • str1 > str2 is sorted in lexicographic order, the string str1 is after the string str2, generally not Will use

    Try the example:

    [root@server1 ~]# A=str[root@server1 ~]# B=str[root@server1 ~]# [ $A = $B ][root@server1 ~]# Echo $?0[root@server1 ~]# B=Str[root@server1 ~]# [ $A = $B ][root@server1 ~]# echo $?1[root@server1 ~]# [ -n $ B ][root@server1 ~]# echo $?0[root@server1 ~]# [ -z $B ][root@server1 ~]# echo $?1

  • Copyright © Windows knowledge All Rights Reserved