2021年7月17日 星期六

[Linux 文章收集] How to Compare Strings in Bash

 Source From Here

Preface
When writing Bash scripts you will often need to compare two strings to check if they are equal or not. Two strings are equal when they have the same length and contain the same sequence of characters.

This tutorial describes how to compare strings in Bash.

Comparison Operators
Comparison operators are operators that compare values and return true or false. When comparing strings in Bash you can use the following operators:
string1 = string2 and string1 == string2
The equality operator returns true if the operands are equal.
- Use the = operator with the test [ command.
- Use the == operator with the [[ command for pattern matching.

string1 != string2
The inequality operator returns true if the operands are not equal.

string1 =~ regex
The regex operator returns true if the left operand matches the extended regular expression on the right.

string1 > string2
The greater than operator returns true if the left operand is greater than the right sorted by lexicographical (alphabetical) order.

string1 < string2
The less than operator returns true if the right operand is greater than the right sorted by lexicographical (alphabetical) order.

-z string
True if the string length is zero.

-n string
True if the string length is non-zero.

Following are a few points to be noted when comparing strings:
* A blank space must be used between the binary operator and the operands.
* Always use double quotes around the variable names to avoid any word splitting or globbing issues.
* Bash does not segregate variables by “type”, variables are treated as integer or string depending on the context

Check if Two Strings are Equal
In most cases, when comparing strings you would want to check whether the strings are equal or not.

The following script uses the if statement and the test [ command to check if the strings are equal or not with the = operator:
  1. #!/bin/bash  
  2.   
  3. VAR1="Linuxize"  
  4. VAR2="Linuxize"  
  5.   
  6. if [ "$VAR1" = "$VAR2" ]; then  
  7.     echo "Strings are equal."  
  8. else  
  9.     echo "Strings are not equal."  
  10. fi  
When the script is executed it will print the following output:
Strings are equal.

Here is another script that takes the input from the user and compares the given strings. In this example, we will use the [[ command and == operator.
  1. #!/bin/bash  
  2.   
  3. read -p "Enter first string: " VAR1  
  4. read -p "Enter second string: " VAR2  
  5.   
  6. if [[ "$VAR1" == "$VAR2" ]]; then  
  7.     echo "Strings are equal."  
  8. else  
  9.     echo "Strings are not equal."  
  10. fi  
Run the script and enter the strings when prompted:
Enter first string: Linuxize
Enter second string: Ubuntu
Strings are not equal.

You can also use the logical and && and or || to compare strings:
  1. [[ "string1" == "string2" ]] && echo "Equal" || echo "Not equal"  
Output:
Not equal


Check if a String Contains a Substring
There are multiple ways to check if a string contains a substring.

One approach is to use surround the substring with asterisk symbols * which means match all characters.
  1. #!/bin/bash  
  2.   
  3. VAR='GNU/Linux is an operating system'  
  4. if [[ $VAR == *"Linux"* ]]; then  
  5.   echo "It's there."  
  6. fi  
The script will echo the following:
It's there.

Another option is to use the regex operator =~ as shown below:
  1. #!/bin/bash  
  2.   
  3. VAR='GNU/Linux is an operating system'  
  4. if [[ $VAR =~ .*Linux.* ]]; then  
  5.   echo "It's there."  
  6. fi  
The period followed by an asterisk .* matches zero or more occurrences any character except a newline character.

Check if a String is Empty
Quite often you will also need to check whether a variable is an empty string or not. You can do this by using the -n and -z operators.
  1. #!/bin/bash  
  2.   
  3. VAR=''  
  4. if [[ -z $VAR ]]; then  
  5.   echo "String is empty."  
  6. fi  
Output:
String is empty.

  1. #!/bin/bash  
  2.   
  3. VAR='Linuxize'  
  4. if [[ -n $VAR ]]; then  
  5.   echo "String is not empty."  
  6. fi  
Output:
String is not empty.

Comparing Strings with the Case Operator
Instead of using the test operators you can also use the case statement to compare strings:
  1. #!/bin/bash  
  2.   
  3. VAR="Arch Linux"  
  4.   
  5. case $VAR in  
  6.   
  7.   "Arch Linux")  
  8.     echo -n "Linuxize matched"  
  9.     ;;  
  10.   
  11.   Fedora | CentOS)  
  12.     echo -n "Red Hat"  
  13.     ;;  
  14. esac  
Output:
Linuxize matched.

Lexicographic Comparison
  1. #!/bin/bash  
  2.   
  3. VAR1="Linuxize"  
  4. VAR2="Ubuntu"  
  5.   
  6. if [[ "$VAR1" > "$VAR2" ]]; then  
  7.     echo "${VAR1} is lexicographically greater then ${VAR2}."  
  8. elif [[ "$VAR1" < "$VAR2" ]]; then  
  9.     echo "${VAR2} is lexicographically greater than ${VAR1}."  
  10. else  
  11.     echo "Strings are equal"  
  12. fi  
The script will output the following:
Ubuntu is lexicographically greater than Linuxize.


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...