The case loop
Up until now, we have dealt with basic commands that allow us to do things we need when trying to write a script, such as looping, branching, breaking, and continuing program flow. A case
loop, the topic of this recipe, is not strictly necessary, since the logic behind it can be created using a multi-nested group of individual if
commands. The reason we are even mentioning this is simply because case
is something that we are going to use a lot in our scripts, and the alternative of using if
statements is both difficult to write and read, and complicated to debug.
Getting ready
One could simply say that a case
loop or case
statement is just another way of writing multiple if then else
tests. Case
is not something that can be used in place of a normal if
statement, but there is a common situation in which a case
statement makes our lives a lot less complicated and our scripts much easier to debug and understand. But before we go into that, we need to understand a...