Objective
Today, when writing a script, found that the previous phase used some of the usage is still need to go to Baidu Search, and find the answer also need to filter, delay writing script time, this article on the comparison between the string and logical judgments are very detailed, reference to other people's work, resource sharing.
This film mainly describes some of the considerations in string comparisons
The main include string equality, unequal, length, empty string, and logical and logical, or the content, see the following script:
[Plain]View PlainCopy
- #!/bin/bash
- # test String Order
- A= "SS"
- b= "ABC"
- c= "SS"
- echo "----------------------------------------------------------------------------"
- echo "Test for string compare with \"!=\ ", \" =\ ", \"-z\ ", \"-n\ ", was a null str; test string is \" $a \ "and \" $b \ ""
- echo "----------------------------------------------------------------------------"
- echo "*************----------*********************"
- echo "-----------"
- echo "Test \" =\ "equals arithmetic, compares two strings equal"
- echo "-----------"
- echo "if [\ $a = \ $b]; Then use = to compare two strings are equal, note is not the = = symbol, note that the space symbol in the statement must be reserved, some of the following statements in the space format and the sentence similar to the "
- if [$a = $b]; Then
- echo "$a = = $b: a = B"
- Else
- echo "-----------"
- echo "Test \" "!=\", not Equal, compares two strings of unequal "
- echo "-----------"
- if [$a! = $c]; Then
- echo "$a = = $c: a!=c"
- Else
- echo "$a = = $c: a ==c"
- Fi
- Fi
- echo "-----------"
- echo "Test \"-z\ ", string length is 0 operation, 0 returns True"
- echo "-----------"
- If [-Z $a]
- Then
- echo "-Z $a: string length 0"
- Else
- echo "-Z $a: string length is not 0"
- Fi
- echo "-----------"
- echo "Test \"-n\, string length not 0 operation, not 0 return true "
- echo "-----------"
- If [-N $a]
- Then
- echo "-N $a: string length not 0"
- Else
- echo "-N $a: string length 0"
- Fi
- echo "-----------"
- echo "Test is a null str"
- echo "-----------"
- If [$a]
- Then
- echo "$a: String not empty"
- Else
- echo "$a: string is empty"
- Fi
- A= "AAA"
- b= "BBB"
- echo "----------------------------------------------------------------------------"
- echo "test string with \" &&\ ", \" | | \ "; Test string is \" $a \ "and \" $b \ ""
- echo "----------------------------------------------------------------------------"
- echo "-----------"
- echo "Test \" &&\ ", Logic and Operations"
- echo "-----------"
- if [[$a = $a && $b = $b]]
- Then
- echo "[[\ $a = \ $a && \ $b = \ $b]] returns True"
- Else
- echo "[[\ $a = \ $a && \ $b = \ $b]] returns false"
- Fi
- echo "-----------"
- echo "Test \" | | \ ", logical OR operation"
- echo "-----------"
- if [[$a = $b | | $b = $b]]
- Then
- echo "[[\ $a = \ $b | | \ $b = \ $b]] returns True "
- Else
- echo "[[\ $a = \ $b | | \ $b = \ $b]] returns false "
- Fi
- echo "*************----------*********************"
Basic string usage in the shell