Join Scott Simpson for an in-depth discussion in this video Working with while and until loops, part of Learning Bash Scripting.
We don't always want to have a loop work on a specific range of values. We might want a loop to continue while some condition is true or false, or until some condition occurs. For that, there's the while and until loops. First let's look at the while loop with a simple example that counts up to 10. Here, I'm using both the integer comparison to loop while i is less than or equal to 10, And doing a little bit of arithmetic to increment the value of I by one. You'll notice that the syntax here is basically the same as the if statement, which of course makes sense because we're asking for an evaluation, that is whether I is less than or equal to ten, and if it evaluates true, then we do everything within the loop.
And then at the end of the loop is the word done. I'll save it and run, and I see the numbers 0 through 10. The until loop is the counter part to the while loop, here I'm echoing the value of J, until J is greater than or equal to 10. And when I run it, I see that the loop ran until J was equal to 10. Keep in the mind, both of these can cause infinite loops, so be careful to check your logic
- What is Bash?
- Managing output with grep, awk, and cut
- Understanding Bash script syntax
- Creating a basic Bash script
- Displaying text with "echo"
- Working with numbers, strings, and arrays
- Reading and writing text files
- Working with loops
- Using functions
- Getting user input during execution
- Ensuring a response
Skill Level Beginner
Linux: Bash Shell and Scriptswith Kevin Dankwardt2h 46m Intermediate
1. Working with the Command Line
2. Building Bash Scripts
3. Control Structures
4. Interacting with the User
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.