linux if judgment

The way to compare whether two strings are equal is:
if  ["$test"x = "test"x ]; then
there are a few key points here:
1 Use a single equal sign
2 Note that there is a space on each side of the equal sign: this It is a requirement of the unix shell.
3 Note that the last x of "$test"x is deliberately arranged, because when $test is empty, the above expression becomes x = testx, which is obviously not equal. And if there is no such x, the expression will report an error: [: =: unary operator expected

binary comparison operator, compare variables or compare numbers. Pay attention to the difference between numbers and strings.
Integer comparison-
eq   is equal, such as: if  [" $a" -eq "$b"] -    
ne is not equal to, such as: if  ["$a" -ne  "$b"] -gt is   greater than, such as: if  ["$a" -gt "$b"] -ge is   greater than or equal to, such as: if  ["$a "-ge "$b"] -lt is   less than, such as: if  ["$a" -lt "$b"] -le is   less than or equal to, such as: if  ["$a" -le "$b"] is  greater than (required Double brackets), such as: (("$a"> "$b")) >=  greater than or equal to (double brackets are required), such as: (("$a" >= "$b")) Small data comparison can be used AWK string comparison  equal to, such as: if  ["$a" = "$b"] ==   equal to, such as: if  ["$a"== "$b" ], equivalent to =  Note: The function of == has different behaviors in [[]] and [], as follows:  1 [[ $a == z* ]]   # If $a Start with "z" (pattern matching) then it will be true     
    
    
    
    
     
    


    
    
     
      
      2 [[ $a == "z*" ]] # If $a is equal to z* (character matching), then the result is true  3  4 [$a == z*]  # File globbing and word splitting will happen  5 [ "$a" == "z*"] # If $a is equal to z* (character matching), then the result is true. A  little explanation about File globbing is a shorthand for files, such as "*.c", Another example is ~.  But file globbing is not a strict regular expression, although the structure is more like in most cases. != is   not equal, such as: if  ["$a" != "$b"]  This operator will Use pattern matching in the [[]] structure.  Greater than, in ASCII alphabetical order. For example: if  [[ "$a"> "$b" ]] if  ["$a" \> "$b"]  Note: In the [] structure ">" needs to be escaped.  Refer to Example 26-11 to see an example of the application of this operator. -z   string is "null". The length is 0. -n   string is not "null" "Digital comparison can also use the following expression method: if ((${d1}==0)) 
     
         
     
     
     
    
     
     
      
      
     
     
    
    



   if((${d1<0}))

Guess you like

Origin blog.51cto.com/593095349/2674609