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 ##################...