We have begun to make progress within this chapter. We have been able to join many of the elements that we have previously used into cohesive and functional scripts. Although the focus of this chapter has been on loops, we have used command-line lists, if statements, case statements, and arithmetic calculations.
We opened this chapter by describing loops as the workhorse of our scripts and we have been able to demonstrate this with for, while, and until loops. The for loop is used to iterate through elements of a list. The list can be either static or dynamic; with an emphasis on dynamic lists, we showed how simply these are created through file globbing or command expansion.
Also, we saw how to iterate over complex values and how to set the IFS to iterate over fields correctly.
We learned how to write nested loops and how to redirect loop output to files.
The while and until loops are controlled using conditions. The while loop will loop while the supplied condition is true. The until loop will loop until the supplied condition returns true or while it returns false. The continue and break keywords are specific to loops and, using them along with exit, we can control the loop flow.
In the next chapter, we will look at modularizing scripts using functions.