Sign up for Linode here: https://ntck.co/linode and you get a $100 Credit good for 60 days as a new user!
Looking to get ripped? Well look no further, because in this episode of NetworkChuck’s BASH scripting course, Chuck is going to show you how to master loops in BASH and how to use them to get you ripped!
0:00 ⏩ Intro
1:04 ⏩ What will you need?
2:25 ⏩ Let’s get started
3:05 ⏩ The While loop
8:33 ⏩ The Until loop
9:51 ⏩ The For loop
16:20 ⏩ Looping back
19:37 ⏩ Outro
This bash script is going to make you
Because in this video, we’re gonna build a push up counter in bash, building up your bash skills and your muscles at the same time. Hi, got time. Watch out. Ladies
Loops allow us to write scripts that repeat loops. Allow us to write scripts that repeat loops. Allow us to write scripts that re whoa got caught in a loop there. Sorry about that. Welcome to bash episode five, where we are going to build up our bash muscles and our real muscles with something called loops. We’re gonna start off with wild loops. We’ll actually use these to build our pushup counter. Then we’ll move on to until loops. And then this one you don’t wanna miss four loops. It’s my stink and favorite. I love these things. Welcome to can Chuck beat the machine? Hold on, need more lights. Can I do 50 pushups before Le node deploys, this spiritual machine ready set, kind of nervous. Go
Now to fall along with this lab, you will need a virtual machine like Lennox from Le node. I use Le node because they’re so fricking fast. Can I do this is still provisioning. Okay. Wicked fast. And I deploy a virtual machine on N every time I have to do a lab. Holy crap, because it cost almost nothing. $5 a month. And the only charge you 0.0, zero, like 75 cents per hour. I’m dying. Did I lose already? Oh crap. I got lost.
How may I get done? Video editor. Oh, freaking a okay. Ode one. But like I said, this lab will only cost you 0.0 75 cents an hour. Use it for an hour, delete it. And it only costs you a penny or it can literally cost you nothing. If you head over to leno.com/network, Chuck, I need some coffee. If you’re new to Leno, you’ll get a hundred dollars credit for 60 days. Just do it. It’s awesome. And all the scripts I’m gonna show you in this video are inside of stack script. And all you have to do is go find it. Just search network. Chuck I’ll also have that as a link below, click on that sucker and select deploy. That’s it. After you put in some info. So go ahead and get your lab going. I’m gonna take coffee break and then we’ll, we’ll get started with bash episode five. So once your Le node is booted, it’s running like this. We’ll go ahead and jump into its terminal by accessing it via SSH. I’m gonna copy this command right here. Copy launch my command prompt or terminal for windows or Mac. I mean Lenox or Mac I’m I’m so tired right now. Pace that command and let’s get started except I fingerprints and put in the password
We’re in now real quick. If you type in LS, you should see how the fun scripts you’re gonna play with there. They all are. Yes. You don’t even know what’s coming. Not no time to waste. Let’s go ahead and write our push up script. We’ll start a clean script. Nano pushups.sh hit enter. And then Ang did I scare you? I’m gonna get you one of these times, put in your shebang at the top. If you’re like, what the junk is that go back and watch the first four episodes in the series you’ll be caught up. So we’re gonna start with the wild loop. The wild loop will loop or repeat your script over and over. As long as something is true, let’s write one real quick and I’ll show you what it does. It’s pretty cool. Let’s first create a variable. We’ll call it X and we’ll have it equal.
The integer one equals one, and then we’ll start our loop. So it’ll actually start with Y if you spell it right, Y there we go. And then right after Y will be our condition. So I’ll try this and type it with me. We’ll do opening brackets, two of them space. Then we’ll call our variable X dollar sign X. And we’ll say, this has to be less than or equal to 100. We’ll do a space closing double brackets. And that’s our condition. This sucker will evaluate the true. So while this sucker is true, do something, and we’re about to tell it to do something right now. So I’ll go ahead and hit, enter, and then type in do, keeping in mind that as I’m doing this, I’m creating space to make it look clean and nice. So you don’t have to do this. And you may see scripts that are just kind of jumbled together, kind of hard to read, but we’re gonna keep it pretty.
So type in do, and then hit enter once more and let’s tell it to do something. I’m gonna hit a couple spaces again, to make it pretty. And I’ll say echo, and we’ll make the echo say this. Hey, I just did the variable X pushups and close that string out. And then we’ll do one more kind of weird thing right after the echo. Don’t worry. It’s not too strange, but just type it with me real quick, we’re gonna do opening parentheses two of them space. Then we’ll type in X and then space plus plus space and closing parentheses. Now all this little weird thing is doing is simply adding one to our X, our variable X plus one, and it’s gonna do something pretty cool. Watch this. Okay, let’s go ahead and close this out. We’ll hit enter, and then to close out our loop to say, we’re done.
We’ll just, we’ll tell it. We’re done type in done. So remember this key thing right here with our wild loops and which with every loop we’re gonna do here in this video, what’s going to be looped is between the do and the done. And you must put those to close it out. So what do you say? We try it. You’re about to get ripped. You ready? So say control X Y enter to save. We’ll make our script executable real quick. We’ll do ch mod plus X, and then the name of our script, pushups.sh. And then we’ll run our script with period slash pushups.sh you ready for this? Hit enter and go, dude, how’d you do so many pushups, easiest pushups you’ve ever done. Take a little coffee break. You did a great job. If you go through and count, you did a hundred pushups, congrats, but you’re still weak
Wey, Wey, Wey you a little beat at work
Because yeah, automation’s cool. Except when it comes to your health, you probably shouldn’t automate your health. Let’s get back into our script and fix that nano pushups.sh. So what did we see there? We saw that we started with our variable X equaling one and with our wild loop, as long as our X variable was less than or equal to a hundred, this loop is gonna go on forever, which obviously it didn’t because inside the loop, we incremented the variable variable X by one, each time it was run, but you’re lazy and weak. Take out the echo, take that sucker out. Now let’s change that to read. And if you recall reading, it’s how we get user input from our script. And the user in this case is gonna be you. You’re gonna be inputting some work here. We’ll type in dash P and they’ll have a say something.
We’ll say push up, we’ll call our variable X, and we’ll say, press enter to continue. And then right at the end, after the done, we’ll give a little congratulatory message. Now, all we’ve done here is required user input. Every time the bash loops and tell you what one hundred’s way too much. Let’s lower that a bit. Just so the sake of our example, we’ll do 10 and that should be good. So let’s do control X, Y enter to save, and let’s try out our script real quick now, because I don’t wanna get down on the ground again. I’m just gonna do some squats. You can do anything you want. Let’s run our script. Three fours. Let’s push up. Sh, here we go. Push up one squat one, enter squat. Two <laugh> enter.
Okay. Oh yeah. Coffee break. I hope you did that with me, by the way. Hey, there, ain’t nothing wrong with getting a little workout while you’re learning. Get that brain pump the gave it talk right now. Get the blood in your brain pumping. Now you probably won’t see too many wild loops forcing you to exercise. So this was a weird use case, but a more practical use case might be something like this. Go ahead and open up. One of the example, files read underscore example, sh this one will look a bit weird, but what this will do is open up a file. And in this case, the file is called Hamlet. It’s literally the entire Shakespeare play Hamlet in one text file. And this wild loop will literally go through each line line by line and echo it out. And as long as there’s a line to read, which is what that condition is saying, it’ll go through it all.
And if I run it, I’ll show you what happens. Bash read example to sh <laugh> did it. Who 5,566 lines. Now let me warn you. Don’t you mess around with wild loops. They can be dangerous. Let me show you let’s start a new script real quick, nano dangerous.sh watch out shebang. Got I got you type in wild and the right after while type in true. This right here is scary. Are you scared? Let me show you why you should be scared. Let’s try it out. Hit enter type in, do enter a couple spaces. Say echo, are you scared? <laugh> enter type in done control X Y enter to save. Now let’s run that sucker bash dangerous to sh you ready for this? It’s going on forever. What just happened there? Well, let’s jump back in there real quick. Remember, the wild loop will run as long as they condition is true.
And when you say wild, true, you’re basically saying while true is true. <laugh> it’s, it’s like, well, true. True. Well, that’s always gonna be true. It’s always going to run. People do use this. So be careful, but control C will always save the day. Now it’s time for the upside down version of the wild loop. The until loop. It’s the exact same thing. Just backwards, upside down. That’s all it is. Let me show you let’s open another example, script real quick, nano until example one sh notice it does look pretty familiar. The main difference being instead of while we have until now a while loop will run while something is true and until loop will run until something is true. So look at our condition real quick. We’re saying until the variable order equals the string coffee, we want you to loop. So this right now, currently isn’t true.
And this will loop until it becomes true. And the look here what’s happening in the loop. It’s gonna echo, would you like coffee or tea? And then we’re going to read, get customer input, get input from you and whatever you type in, we’ll be put into the variable order. So I’ll ask you, Hey, do you want coffee or tea? As long as your order, the variable order doesn’t equal coffee. This single will loop. Let’s try it out. Control X, get outta there. We’ll run it with bash until example, sh would you like coffee or tea? Let’s type in tea? Well, that didn’t work because we didn’t type in coffee. Let’s try juice now. Milk. No, but if we type in coffee, ah, finally, excellent choice. Here is your coffee. That’s the only right answer. So jumping in there real quick, once more until as just the upside down of while now, time for the fun stuff, four loops these things you can get lost in let’s hours, playing with it, looping with it.
Don’t get yourself caught in a four loop. <laugh> let’s try it out though. Let’s go ahead and write a new script. So nano we’ll call this four HBE, sorry. Now, to start a four loop, you’re probably guessed it. We’re just gonna start typing in four, instead of until, or while the four loop. He’s not lazy. He’s not gonna wait for something to be true or false. He wants a list of things to do. So let’s give him a to do list real quick. Let me show you so right after four, let’s create a variable. Let’s just call it cups. And then we’ll say N and this is gonna be kinda weird. One space, two space, 3, 4, 5, 6, 7, 8, 9, 10, and then semicolon at the end. What is happening? <laugh> cause I know it was kinda weird. And if this does feel weird, don’t feel bad. This took a bit for me to get it.
When I first learned it, what’re saying here is our variable cups will become each of the items in this list. That’s what it’s gonna do. And it will loop through the script as each variable. So for one loop cups will equal one for the next loop. It’ll equal 2, 3, 4, and it will loop through the entire list until it gets to 10 and in the loop is done. So let’s do something it’ll make more sense when we do stuff. So just after the semicolon, let’s hit enter just like our while and until loops will type in, do hit enter, give it a bit of space and we’ll say echo, Hey, you’ve had, and let’s go ahead and call that variable cups, dollar sign cups. And then we’ll say, cups of coffee today. Then we’ll hit enter. And then at the end of our loop, just like while, until we’ll type in done.
So when we run this, this variable cups will become each of the items in this list. It’ll do it 10 times. Let’s try it out. Hey, control X Y enter to save and we’ll do bash four.sh to run our script. Isn’t that cool. Look at that. Each time the cups variable changed his identity based on our list in one loop, he was one in another loop. He was seven and that’s the power of four loops is kind of crazy. Now in just a moment, I wanna show you a four loop. That’ll allow us to check the connectivity of different websites. It’s very cool. And it shows the power of four loops. Lemme show you one thing real quick. Let’s jump back into our four sh file and let’s go to the top to where our numbers are. Our 1, 2, 3, 4, 5. You know the list. Let’s take it out.
I wanna show you a different way. We can list these numbers. We can do a range instead of just listing them individually, by doing an opening curly brace typing in the first number, doing a.dot and then the last number, and then closing curly brace. And the reason I wanted to show you that is because you’ll probably see it in the wild and go what’s going on there. So I want you to be aware. So if we save this, so X Y enter to save, run the script once more, does the same thing, not time for the internet thing, we’re gonna check and see if certain websites are up with the, for loop. And then I’ll show you one with the weather like it’s it’s nuts. Four loops are endlessly fun. So go ahead and open another example file. We’ll do nano four underscore loop, ping domains.
St. It looks just like this. Go ahead and open that file right now. Now this one’s pretty cool. And I hope it gets the gears kind of turning in your brain of like, what else you can do with this. But here’s, what’s happening. Notice we’re starting the same way. We have our four and we’re calling instead of cups, we’re doing the variable X, and we’re saying four X in a list of websites. And remember what it does here. Every time this loop will run, X will become a different thing in that list. And one loop X will become Google and the next loop X will become B. And in that loop here is what’s happening and notice we’ve done something kinda weird. We have an if statement, a conditional, we’re combining the knowledge of episode four and five. What? And it’s not too crazy. What it’s doing, all it’s doing is saying, Hey, if, and then we’re gonna ping it.
It’s gonna ping with a few options. Nothing too crazy. We’re just saying, Hey, make it quiet. Let’s just do two pings. And we’re only gonna wait for like one second to receive those pings. And then what we’re pinging is the variable X, which remember X will be Google or ping or Facebook or network. Chuck it’ll be all of those as it goes through. And then all we’re saying right here is we want the ping output to not be there. We don’t wanna see the individual responses when you ping something. And what’s cool about this one statement. The, if ping, just this guy right here, all it’s saying is if we receive a response back when we ping something, it’s true. So if it is true, then go ahead and do this action. We say, then do this. If we don’t receive a ping back, then it will do our else statement. And basically we’re gonna say the domain is up or the domain is down. It’ll go through our list and then be done. Let’s try it out real quick. Control X, get outta there. We’ll do bash four loop, ping domains.
Bam. Isn’t that cool. And then I went ahead and added a host. I know it was down. And then bam, look at that. Try to ping it. It’s down. So you could add a list of your favorite websites or maybe your company’s websites and just kind of do a quick little, let’s see if they’re up just for fun. And then here’s a really fun one. Go ahead and open up for_weather.sh this one, I love this one. Look at our for loop. We don’t have a list there. Instead we have a command <laugh> yes. We have a command. That’s opening up a file. What? <laugh> yes, you can do that. I told you for loops are crazy. So actually let’s, let’s back out real quick. I’m getting too excited. Let’s back out. Let’s run that command. Let’s cat cities dot TXT. This is a file that should be there.
If you launched my stack script in Le node inside cities dot TXT is a list of cities shocker, right? So when that four loop runs that command, it’s going be looking at this list. So when I say for X, N blah, blah, blah, X will become each of these cities in that file. How cool is that? All right, anyways, let’s get to our file. Open it once more, nano four weather sh. And what this will do is something that we’ve used in a previous episode in the series, it’ll use the WTT R dot N um, curl interface. We can actually curl the current weather for a city, getting the weather and your terminal, which is just really nerdy and awesome. So here’s what we’re doing for X, our variable in cities. So X will become each city in that text file. It’ll run this command, noticing that inside of our URL, we have the variable X right there, and then it’ll echo out the weather for that city.
Let’s try it out real quick. Bash four weather.sh. Here we go. Bam. Look at that. A quick weather forecast of all the cities you could possibly imagine just like that. Now this is silly fun, but I hope it kind of paints the picture of what a four loop can do. And frankly, while loops and until loops, they are all extremely powerful. So yeah. Have fun with it. Practice dump, stuff like this, because the things you’re doing here can be used in a real world business context. Trust me. It’s awesome. Now real quick. I wanna loop back to wild loops cause I wanna show you two concepts that really apply to all these loops, but it’ll be our break and our continue. These are two concepts we can use inside of our loops to change the behavior. And remember earlier I said that using a wild loop with the wild true can be very dangerous.
It can be, but using with break is actually something people do like watch this. I have a little script here that will check the up status. Like is a site up or is an IP address up with the wild loop? Let’s try it out. So let’s do nano for internet and internet down. Toch it’s actually a wild loop. I don’t know why I call it four, but just ignore that. And here we go. Pretty simple opening. It’s gonna ask you, Hey, what website or IP do you wanna check? It’s gonna ask you for it in store it in the target variable and then whew, here’s that dangerous statement? Wild. True. This will run forever. Run forever unless you stop it or unless we break it. That’s what happens here. Notice we have an, if conditional here and we have that same ping that we did from earlier.
So if what we ping does respond, that’s automatically true and it will do this. It’ll say, Hey, you’re up and then break. Get outta there. Bust out that script. The loop is done. That’s what break does otherwise or else it’ll tell you, Hey, it’s down and it will keep looping notice. I have a sleep two. It’ll wait two seconds, then loop again. Then loop again until that one thing is up. So I actually have another server in Le node that is certainly down right now. Let’s test it out with that. So let’s bash four internets down, sh run that sucker. What do I wanna check? I’m gonna throw in this IP address and let it run. It’s currently down. And it’s gonna tell me that until it’s up, which I think is very cool. So I don’t know about you, but sometimes I’ll be rebooting my things, whether it’s an internal IP address or something, external could be a domain or whatever.
And I’m like, okay, I’m gonna reboot it way for it to come up. And this is kind of a cleaner way to do this versus just having a constant, continuous ping. So I’m gonna go ahead and boot it up real quick. I’m not gonna make you wait, just take a sip of coffee and you’ll see it in. Like, I dunno, you’ll see it now. So, whereas a continuous ping would just go on forever. Uh, this stops as soon as it got an up response, it broke out of the script break, which that’s super useful and handy now for the next one. Continue this one’s cool. And I love the example that Sean Powers used. So thank you Sean for this. And it’s an example of an elevator. So go ahead and open up the next script. Nano elevator example.sh here we have another four loop and it should look very, very familiar.
We’re doing a simple couple echoes here at the beginning. We’re looping through a sequence of numbers that we’re defining right here, but then we have something curious here. We have an, if conditional, if the variable X equals 13, then continue. What does that mean? It means we’re going to skip that loop. SOAs break will just break out and be done. Continue is just a simple skip. It’ll skip that loop and move on to the next loop, which is super handy. Let’s try it out real quick. Bash elevator example, the sh welcome to the hotel. We’re going up floor one floor two, and video editor. Go ahead and speed up a little bit until we get to 11, 12, it’s coming up watch. So 10, 11, 12, 12, no 13. You see in our condition, we said, if it equals 13, continue, skip that loop. And it did that.
Get your before and after pictures in order, you’re about to get ripped from your wild loop. I hope you do build that out and just work out with it. It’s not a bad idea. Also, if you’re doing it like a computer or a laptop and get that isometric, I, I can’t even say it. Isometric thing going on, try it out. Be a nerd. It’s awesome. And get ripped in the process. But anyways, while until, and four loops are essential bash, scripting, things that you’re gonna use all the time, they truly do unlock the power of bash scripting and how you can just do things faster, better. And they’re simply just, they’re fun. <laugh> they’re really fun. So go crazy with it. In fact, I would love to see your scripts. So if you do something really cool, fun, inventive, or just something dumb that you wanna show me, I wanna see it.
Let me see it. Show me on Twitter, tag me or something. Send me your GitHub. I wanna see your scripts. And if the post ups work out, man, send me some after photos. I wanna see those guns. Yeah. I would like to take it to the gun show. So anyways, that’s all I had today. Thanks for having some coffee with me and talking about some bash scripting. Oh wait, almost forgot. Have you hacked the YouTube algorithm today? Let’s make sure you do hit that like button notification, but comment. Subscribe. You gotta hack YouTube today. Ethically of course. And yeah, I’ve been recording too long and I’m sore and I’m tired. This one took a lot outta me. So you’re welcome. Get you guys next time.