image
Breaking and Continuing Loops
Start course
Difficulty
Intermediate
Duration
3h 7m
Students
432
Ratings
5/5
starstarstarstarstar
Description

In this course, we will explore how to set up a network of virtual machines and how to implement SSH key authentication and execute commands on remote systems. We'll look at how to install and remove software from local and remote systems. You'll learn about continue and break statement and their benefits and use cases. You learn how to automate processes on Linux through the use of cron jobs and examine running processes.

This course is part of the Linux Shell Scripting learning path. To follow along with this course, you can download all the necessary resources here.

Learning Objectives

  • Learn how to create a network of virtual machines and how to configure SSH key authentication and execute commands on remote systems via SSH
  • Learn how to install and remove software packages both on your local system as well as on remote systems
  • Understand continue and break statements in loops and what they're used for
  • Understand what cron is and how to use it to schedule the running of scripts in Linux at various intervals
  • Learn how to examine running processes on a Linux system and how to determine their process IDs

Intended Audience

  • Anyone who wants to learn Linux shell scripting 
  • Linux system administrators, developers, or programmers

Prerequisites

To get the most out of this course, you should have a basic understanding of the Linux command line.

Transcript

Before you move on to the next shell scripting project, I want to quickly talk to you about the use of the continue and break statements in loops. Let's say you're looping through a list of items performing a series of actions for each item. However, there are some items in that list you simply want to skip. For example, let's say you have a script that adds a set of users to a system. And for each one of those users, you create their home directory, set a password and do some other things, right? However, if a user already exists, there's no need to do anything for that given user, as they already have a home directory, they have a password and so on, they're already configured. What you want to do in that situation is continue on to the next user and create their account if it's needed and so on. The command to do that continue on to the next iteration of the loop, is conveniently enough called, continue. Now let's write a script that demonstrates this scenario and use the continue command in it. So let's just create a script called, multinet-demo03. Do a little shebang here. So, let's create a set of users here, just a list. By the way, I'm not going to use an array. I rarely ever do, I don't think they're needed in most cases, especially when you just have a simple list like this and you can just separate it by some white space and then use it in a for-loop, so let's do that here. So let's just label this as our continue example. And then we'll do this for user in this list of users. We'll go ahead and check to see if the user already exists. One way to test if a user exists, is to simply run the ID command against their username and then if the user does exist, then the command runs successfully and returns a UID and if it doesn't exist, if the user doesn't exist, then the command does not execute successfully. So we can just simply check the exit status of the command. So let's do that here. So let's run ID against a user. We're not interested in the output of this command. We'll just send that to dev/null. If ID returns and exit status is zero, that means that the command completed successfully and that could only mean that the user exists, otherwise it would be an error. So, what we can do here, is just say that the user exists. And then we can just use the continue command or the continue shell built in, to go ahead and proceed to the next iteration of this a loop. And then I'm just going to put a statement here that represents the work that we would have done, let's say in, a real script or a real world scenario. Okay, I've saved my changes. I'm going to go ahead and make it executable. And let's execute it and see what happens. So the first time around the loop, the variable user, is set to jason. The ID command is run against that user. And apparently that command failed, because it skipped below that, if statement, and went on to quote, create the account. Now, of course we didn't create the account, we just have an echo statement there, kind of a stub for our demonstration purposes here. So then the next time around the loop, user was set to the value of vagrant. The ID command succeeded and so the vagrant user exists. So we went ahead and continued on to the next account. So we didn't try to create or do anything with the vagrant account. And then, so we got to the ellen account and that one didn't exist, so we continue doing it and so on. So that is a demonstration of the continue command in a loop. Let's say you're looping through a list of items and when you come across a certain item or a certain condition, you want to exit or stop that loop entirely and continue on with your script after the loop. To use a non-computer example, let's say that you're looking for your cell phone because you want to call a friend and you have a list of rooms where your phone might be. Your phone might be in the living room. It could be in the kitchen, or maybe it's in the bedroom. If you go to the living room and find your phone, well there's no need to continue onto the kitchen, or definitely not the bedroom. You've effectively broken out of the loop, because you've satisfied your requirement and you met your goal of finding your phone. However, if you didn't find your phone in the living room you would continue on to the kitchen. And if you didn't find it there, you would continue onto the bedroom, and so on. Now, if we bring it back to this list of users we have here, let's pretend that if any of these users exist, then we're satisfied. Then we found what we're looking for. So let's go ahead and code up this example. Let's echo of blank line there and then we'll just say, we're working on the, break example. Do another loop here. So here again, we're just looping through this list of users, but this time if we find that a user exists, and we know that, because the ID command returns a zero exit status meaning it was successful. If we find a user that exists, then we're going to break out of this entire loop and not continue processing any other users. So let's save our changes and execute this and see what happens. So we already went over the continued example at the top. So at the example, at the bottom, it goes through the loop, does an ID against jason. ID doesn't return a zero, so then it just continues on with a loop. The next time user is assigned the value of vagrant and this time the ID command succeeds. And then we break out of the loop and we don't even attempt to try the ellen account. Actually let's make that a little bit clearer. Let's actually do this. Let's say, echo testing user. And then let's execute our script again. So here we see we're testing jason, the loop continues. Then we're testing vagrant and then vagrant exists. And so we break out of the loop. And let's go beyond that loop and while there's nothing else to our script, so the script exits. The point is we never got to the third user in that list, because we used break. Even if the second example might be a stretch in the terms of practicality, you can clearly see the difference between continue and break. And that's what I want you to take away from this lesson today.

About the Author
Students
14199
Courses
61
Learning Paths
13

Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.

Covered Topics