In this lecture, we'll cover the basics of While Loops in Bash. The while loop is used to execute a series 1
of commands as long as a specific condition is True. 2
The while statement starts with the while keyword, followed by a conditional expression. 3
The condition is evaluated before executing the commands and if it's true the commands between do and 4
done are executed. Otherwise, if the condition evaluates to false, the loop is terminated and the 5
program control will be passed 6
to the command that follows the done keyword. 7
Let's try a simple example! 8
I'm creating a new script. 9
I'm declaring a new variable, i, which is equal to 0 and the while loop: while [[ 10
i -lt 10 ]] do and done 11
The piece of code between do and done will be executed as long as i is less than 10, so echo "i: $i" 12
I want to see its value; and in order to avoid an infinite loop, which is a loop that never ends, I have 13
to increment it by one. 14
Bash has limited support for arithmetic operations; to perform arithmetic operations, use double parentheses 15
like this i++ By the way, this is the same as let i = i+1 16
I'm saving the script and setting the execution permission. 17
And I'm running it in another terminal. 18
And we see how the i variable takes the value from 0 to 9 one by one. 19
OK, I've talked about arithmetic operations, so let's take a look at another one i=5 20
b=6, and I want to store the result of a+b in another variable c= and a command 21
substitution. 22
So $((a+b)) 23
OK, when doing arithmetic operations use double parentheses like this. 24
and echo $c; 25
And we see the result of a +b stored in the c variable. This command substitution is the same as 26
let d = a+b 27
Let's move on to another example. As I said, an infinite loop is a loop that repeats indefinitely 28
and never terminates. 29
You get an infinite loop if the while condition always evaluates to true. 30
Let's create another script: infinite loop. 31
So while and a condition that is always true, for example, 1is equal to 1 32
This is one way of creating an infinite loop. Since 1 is always equal to 1, do and done 33
echo "Infinite loop, press to exit." 34
Let's see how it works. 35
OK, I have forgotten a space before the closing bracket, here. 36
I'm saving the script and running it again. 37
And it's working. 38
I have to press control c to exit the script; it will never terminate otherwise. By the way, instead 39
of writing such a condition, 1 equals 1 I can write while true like this. 40
I am commenting out of this line and I'm writing while true. 41
This is the same. 42
Most of the time, an infinite loop is a programming error, but there are cases when it's useful. 43
For example, I want to check continuously every three seconds if a specific process is running. 44
Imagine that you have a server application that should be continuously monitored. 45
You cannot do it with a chron job because the minute is the minimum available time period. 46
So while a white space, colon ( this is another way to create an infinite loop; the colon symbol is 47
a bash built in command that always returns 48
true, you can also use the true keyword written in lowercase) and done And I am doing a command substitution. 49
The process that will be checked, if it's running, will be the first argument of the script 50
so $1 And I'm searching for the process using the pgrep command. And the output of the command will 51
be stored in a variable called output. 52
So output="$(pgrep -l $1)" 53
If the process is running, the string variable called output will not be empty, but if it's not running, 54
the output will be a string of zero length. 55
So it makes sense to use a string testing conditions like this. 56
if [[ -n "$output" ]] 57
This means if the output string is not empty, so the process is running, then echo "The process 58
$1 is running" and I want the process name to be printed out between double quotes. 59
But in order to print to the double quote character, which is a special one, I have to escape it 60
like this. 61
OK, else echo 62
"The process is not running." 63
and fi 64
And I'm sleeping for three seconds. 65
I wanted to check for the process every three seconds, so after three seconds, the piece of code between 66
do and done will be executed again. That's all! 67
I'm saving the script and setting the execution permission. 68
I'm testing it by giving different processes as arguments. 69
This is the script, so let's see if sshd running, system stl status ssh and it's active and running. 70
So pgrep sshd will return a string. 71
So process_monitoring.sh sshd and the process is running. 72
It's checking every three seconds and in another terminal I'll stop the process. 73
systems stl stop ssh 74
And the process is not running. 75
I'm starting it again, 76
perfect! 77
It's working just fine! I'm running another command sleep 100 and I'm checking for the process. 78
And the process is running, but if I kill the sleep process, it will print out "the process is not running." 79
Sometimes you'll see short and simple while loops written on a single line. 80
However, I don't recommend it since it's not readable at all. Just to be aware of it 81
let's write the first infinite loop on a single line directly in the terminal. 82
Remember that anything you write in the script can be also written directly in the terminal. 83
while true; do echo "infinite loop"; sleep 1; done 84
And it's working!