When automating different common administrative tasks using Shell scripts, you will often need to compare 1
the output of a command, which is a string, to another string value, to see if they are equal or not, 2
and based on the result to act accordingly. 3
Let's see how to compare strings in Bash. Two strings are equal when they have the same length and contain the 4
the same sequence of characters in the same order. 5
The comparison operator is the single equal sign if you use the if statement with single square brackets 6
or the double equals sign if you use the if statement with double square brackets. 7
Let's go straight to an example! 8
I'm readting the strings from the keyboard read -p string1 9
The name of the variable will be str1 and the second string value 10
First, I'll use the if statement with single brackets. 11
So if [ "$str1" = "$str2" ] don't forget to 12
quote string variables in if testing conditions to avoid the word splitting or other globbing issues 13
and a blank space must be used between the operator and the operants. The equal sign 14
is the operator and str1 15
and str2 the operands. And I'm displaying a message. 16
The strings are equal. 17
else echo "The strings are NOT equal." 18
and fi and the same piece of code using the if statement with the double square brackets. 19
So if [[ $str1 == $str2 ]] 20
then and the same messages, I'm copying and pasting these lines of code. 21
I'm saving the script, setting the execution permission and running it in another terminal. 22
Let's say abc for str1 and the same string value for str2; and the strings are equal. 23
Let's run the script again, this time giving string values which are not equal. 24
There's also the inequality operator that returns true if the strings are not equal; as in any other 25
programming language 26
this operator consists of an exclamation mark and an equal sign. 27
So if [[ "$str1" != "$str2" ]] by the way, instead of writing the then keyboard 28
on a new line, you could write a semicolon followed by then ; it's the same and you'll see many examples 29
that use this style of programming echo 30
"The strings are not equal." 31
And fi I'm saving and running the script. 32
Let's move on and see how to check if a string contains a substring; there are multiple ways to do 33
that 34
but one approach is to surround the substring with asterisk symbols, which means "match all characters". 35
In fact, I'm going to create a new script for this example. 36
substring.sh 37
So str1="Nowadays, Linux powers the servers of the Internet." 38
And I want to check if str1 contains the substring Linux, so if [[ “$str1” 39
== *"Linux"* ]] 40
Like this! 41
Don't forget a white space before the closing square brackets; and then echo 'The substring Linux is 42
there" 43
else "The substring Linux is not there." 44
I'm saving the script, setting the execution permission and running it. 45
And the message is "The substring Linux is there." Now, I'll use a lowercase l, of course, this string 46
does not belong to str1. 47
Sometimes you want to check if a string resulting from another operation, like a command substitution 48
is empty or not. You can do this by using an if statement with -n and -z operators; 49
-n returns 50
true, if the string length is non-zero and the -z returns true if the string length is zero. 51
Let's an example! 52
I'm creating a new script called empty string, 53
so my_str= and a random string value ”abc” and I'm testing if the string is zero length, so 54
if [[ -z "$my_str" ]] then echo "String is zero length." 55
else echo "String is not a zero length." 56
and fi. And another if statement that uses -n for testing: -n will return true if the string 57
length is not zero, so the string is not empty. 58
So if -n and the string variable 59
then echo "String is not a zero length." 60
And else "Echo String is zero length." 61
And fi I'm saving the file, setting the execution permission and running it. 62
Sorry, there is one mistake, of course, in the second if statement, we are using -n, not -z 63
I'm running the script again. 64
Perfect, let's make the string zero length. 65
And the result is the expected one. That's alll for the moment, we'll take a short break and in the next lecture, 66
I'll show you a real-life example of a script that uses flow control, command substitution, and string checking.