This chapter was dedicated to conditional testing and scripting loops. Since we had already discussed the if-then-else statements, we recapped on this information before going on to showing more advanced uses of the conditional testing toolkit. This advanced information included using regular expressions, which we learned about in the previous chapter, within a conditional testing scenario to allow for more flexible tests. We also showed you how multiple conditions can be tested sequentially, using elif (short for else if). We explained how multiple if-then-else statements can be nested to create advanced logic.
In the second part of this chapter, we introduced the while loop. We showed you how we can use this to create a script that will run indefinitely, or how we can use conditions to stop the loop when a certain criteria has been met. We presented the until keyword, which has the same functionality as while but allows for negative checking instead of positive for while. We ended the explanation on while by showing you how an interactive script can be created in an unending while loop (using our old friend read).
After while, we introduced the more powerful for loop. This loop can do the same things while can, but often the shorter syntax allows us to write less code (and more readable code, which is still a very important aspect in scripting!). We showed you how for can iterate over a list, and how we can create a list of numbers using brace expansion. We ended our discussion on for loops by giving a practical example of combining for with file globbing patterns to allow us to dynamically find, grab, and process files.
We ended this chapter by explaining loop control, which is achieved in Bash with the break and continue keywords. These keywords allows us to break out of a loop (even from nested loops, as far back outside as we need), and also allow us to stop the current iteration of the loop and continue to the next iteration.
The following commands/keywords were introduced in this chapter: elif, help, while, sleep, for, basename, break, and continue.