The while loop

Now that we've got the if-then-else recap and advanced usage out of the way, it is time to discuss the first scripting loop: while. Look at the following definition, which should seem familiar after if-then-else:

WHILE condition-is-true DO thing-to-do DONE

The biggest difference between if and while is that while will perform the action many times, so long as the condition specified is still true. Because it is often not needed to have an unending loop, the action will regularly mutate something related at the condition. This basically means that the action in do will eventually cause the while condition to be false instead of true. Let's look at a simple example:

reader@ubuntu:~/scripts/chapter_11$ vim while-simple.sh 
reader@ubuntu:~/scripts/chapter_11$ cat while-simple.sh
#!/bin/bash

#####################################
# Author: Sebastiaan Tammer
# Version: v1.0.0
# Date: 2018-10-27
# Description: Example of a while loop.
# Usage: ./while-simple.sh
#####################################

# Infinite while loop.
while true; do
echo "Hello!"
sleep 1 # Wait for 1 second.
done

This example is the most basic form of while: an unending loop (because the condition is simply true) that prints a message and then sleeps for one second. This new command, sleep, is often used within loops (both while and for) to wait for a specified time. In this case, we run sleep 1, which waits a single second before going back to the top of the loop and printing Hello! again. Be sure to try it out and notice how it will never stop (Ctrl + C will, however, kill the process, since it's interactive).

Now we'll create a script that will end at certain time. To do this, we'll define a variable outside of the while loop, which we will use as a counter. This counter will be incremented at each run of the while loop, until the threshold defined in the condition is reached. Take a look:

reader@ubuntu:~/scripts/chapter_11$ vim while-counter.sh 
reader@ubuntu:~/scripts/chapter_11$ cat while-counter.sh
cat while-counter.sh
#!/bin/bash

#####################################
# Author: Sebastiaan Tammer
# Version: v1.0.0
# Date: 2018-10-27
# Description: Example of a while loop with a counter.
# Usage: ./while-counter.sh
#####################################

# Define the counter outside of the loop so we don't reset it for
# every run in the loop.
counter=0

# This loop runs 10 times.
while [[ ${counter} -lt 10 ]]; do
counter=$((counter+1)) # Increment the counter by 1.
echo "Hello! This is loop number ${counter}."
sleep 1
done

# After the while-loop finishes, print a goodbye message.
echo "All done, thanks for tuning in!"

This script should be self-explanatory because of the comments we've added. counter is added outside the while loop, because otherwise every run of the loop would start with counter=0, which would reset the progress. As long as the counter is less than 10, we'll keep running the loop. After 10 runs, this is no longer the case, and instead of going back in the loop, we're going to the next instruction in the script, which is printing the goodbye message. Go ahead and run this script. Edit the number after sleep (hint: it also accepts values smaller than a second), or remove sleep altogether.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.164.24