Planning Now that your scripts are getting a little more complex you will probably want to spend a little bit of time thinking about how you structure them before diving in. Pick the one which makes your code the easiest to follow. Here is a simple example to illustrate it's usage:Ĭlarity There are several Bash loop mechanisms. You may change the system variable PS3 to change the prompt that is displayed.The loop will end when an EOF signal is entered or the break statement is issued.If the user hits enter without entering any data then the list of options will be displayed again.If the user enters something other than a number or a number not corresponding to an item then var becomes null (empty) Once finished a prompt will be displayed again so the user may select another option. When they select a number and hit enter the corresponding item will be assigned to the variable var and the commands between do and done are run. A prompt will be printed after this allowing the user to select a number. When invoked it will take all the items in list (similar to other loops this is a space separated set of items) and present them on the screen with a number before each item. The select mechanism allows you to create a simple menu system. So by having both while and until we can pick whichever one makes the most sense to us and as a result, end up with code that is easier for us to understand when we read it. Leave the towel on the line while it is wet.īut they just don't seem as elegant and easy to understand. Leave the towel on the line while it is not dry. Leave the towel on the line until it's dry. Sometimes, however, it just makes it a little easier to read if we phrase it with until rather than while. The while loop would be able to handle every scenario. So you may be asking, 'Why bother having the two different kinds of loops?'. We can also create a script that does exactly the same as the while example above just by changing the test accordingly. As you can see in the example above, the syntax is almost exactly the same as the while loop (just replace while with until).
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |