Loop control
At this point, you should feel comfortable with using while
and for
loops. There is one more, rather important, topic to discuss with regards to loops: loop control. Loop control is a generic term, for anything that you do to, well, control the loop! However, there are two keywords that we'll need if we want to unleash the full power of loops: break
and continue
. We'll start with break
.
Breaking the loop
For some scripting logic, it will prove necessary to break out of the loop. You might imagine that, in one of your scripts, you are waiting for something to finish. As soon as that happens, you want to do something. Waiting and periodically checking inside a while true
loop could be an option for this, but if you recall in the while-interactive.sh
script, we exited on the successful answer to the riddle. On an exit, we cannot run any more commands that are outside of the while
loop! This is where break
comes into play. It allows us to exit the loop, but continue the script. First...