a BASH script PUSH-UP counter (for #gains )

NetworkChuck
24 Aug 202221:07

Summary

TLDRThis video script presents a unique blend of fitness and coding by guiding viewers through the creation of a push-up counter using Bash scripting. The tutorial kicks off with an introduction to 'while' loops, which are employed to build the push-up counter, and then moves on to 'until' and 'for' loops. The script is interactive, incorporating user input and demonstrating practical applications like checking website connectivity and fetching weather updates for a list of cities. The video also humorously highlights the potential dangers of infinite loops and showcases the use of 'break' and 'continue' statements to control loop behavior. The presenter encourages viewers to experiment with loops, emphasizing their power and utility in real-world scripting scenarios.

Takeaways

  • πŸ’ͺ Building a push-up counter in bash can help improve both your bash skills and physical fitness.
  • πŸ” Loops in bash scripting allow for repetitive tasks, enhancing the efficiency of your scripts.
  • πŸ“ˆ The 'while' loop continues to execute as long as a specified condition is true.
  • πŸ›‘ The 'until' loop is the inverse of the 'while' loop, running until a condition becomes true.
  • πŸ”’ The 'for' loop iterates over a list of items, making it useful for executing a block of code multiple times with different values.
  • 🚫 Be cautious with 'while true' loops as they can create infinite loops if not properly managed.
  • πŸ› οΈ Using 'break' can exit a loop prematurely, which is useful for stopping an infinite loop when a condition is met.
  • πŸ”„ The 'continue' keyword can skip the current iteration of a loop and move to the next one.
  • 🌐 For loops can be used to check the connectivity of different websites by iterating over a list of domain names.
  • β˜• Taking breaks and incorporating real-world examples, like checking the status of a server, can make learning bash scripting more engaging.
  • 🌟 Combining bash scripting with practical applications, such as checking website statuses or fetching weather updates, demonstrates the real-world utility of the skill.

Q & A

  • What is the main topic of the video script?

    -The main topic of the video script is building a push-up counter using Bash scripting while learning about different types of loops in Bash.

  • Which types of loops are covered in the video?

    -The video covers while loops, until loops, and for loops.

  • What is the purpose of the while loop in the push-up counter script?

    -The while loop in the push-up counter script repeats the script as long as a specified condition is true, in this case, counting push-ups until a certain number is reached.

  • How does the script increment the push-up count?

    -The script increments the push-up count using the X++ syntax within the while loop.

  • What change is made to the script to require user input for each push-up?

    -The echo command is replaced with a read command that prompts the user to press enter to continue after each push-up.

  • What is the difference between a while loop and an until loop?

    -A while loop runs as long as a condition is true, whereas an until loop runs until a condition becomes true.

  • How is a for loop different from while and until loops?

    -A for loop iterates over a list of items, executing the script for each item in the list, unlike while and until loops which depend on conditions being true or false.

  • What does the break command do in a loop?

    -The break command exits the loop immediately, stopping any further iterations.

  • What is the purpose of the continue command in a loop?

    -The continue command skips the current iteration of the loop and moves on to the next iteration.

  • How can you use a for loop to check the connectivity of different websites?

    -A for loop can be used to iterate over a list of websites and use the ping command to check if each website is up, printing the status for each site.

  • What is the significance of the 'shebang' at the top of the script?

    -The 'shebang' (#!/bin/bash) specifies the script interpreter to be used, in this case, Bash.

  • What does the command 'chmod +x pushups.sh' do?

    -The command 'chmod +x pushups.sh' makes the script executable, allowing it to be run as a program.

  • How is user input handled in the modified push-up counter script?

    -User input is handled using the read command, which pauses the script and waits for the user to press enter before continuing.

  • What does the variable X represent in the push-up counter script?

    -In the push-up counter script, the variable X represents the current count of push-ups.

  • What happens if you run a while true loop without a break condition?

    -Running a while true loop without a break condition will result in an infinite loop, causing the script to run indefinitely.

  • How is the ping command used within the for loop to check website status?

    -The ping command is used with options to make it quiet and to only send two pings with a one-second timeout. The script then checks if a response is received to determine if the website is up.

  • What is the purpose of the sleep command in the wild loop example?

    -The sleep command pauses the script for a specified number of seconds (2 seconds in this case) before the next iteration of the loop.

  • What does the script do if it detects a website or IP address is up during the while true loop?

    -If the script detects a website or IP address is up during the while true loop, it prints a message indicating the site is up and then uses the break command to exit the loop.

  • How does the for loop handle a range of numbers in the example script?

    -The for loop handles a range of numbers by using curly braces and specifying the start and end of the range (e.g., {1..10}), which generates a list of numbers for the loop to iterate over.

  • What happens if the condition in an until loop is initially true?

    -If the condition in an until loop is initially true, the loop will not execute because the condition is already met.

Outlines

00:00

πŸ’ͺ Building a Push Up Counter with Bash Loops

The script's purpose is to create a push up counter in bash, combining physical exercise with scripting practice. The video introduces the concept of loops in bash scripting, starting with 'while' loops to build the push up counter. It then discusses 'until' loops and 'for' loops, with a special mention of 'for' loops being the presenter's favorite. The video also includes a playful challenge where the presenter races to do 50 pushups before a virtual machine deployment completes. The use of Le node for virtual machine deployment is highlighted for its speed and cost-effectiveness, with a special offer mentioned for new users. The script writing process begins with setting up a new bash script file and adding a shebang line, followed by the implementation of a 'while' loop with a variable 'X' that increments to count pushups.

05:02

πŸ” Automating Pushups with While and Until Loops

The paragraph demonstrates the creation of a bash script that uses a 'while' loop to automate counting pushups, with an initial setup that counts up to a hundred pushups. The script is made executable and run to show the loop in action. The presenter then modifies the script to use 'read' for user input, allowing the user to decide when to stop the pushup count. The script is rerun to illustrate user interaction. Additionally, a cautionary tale about infinite loops is shared, showing what happens when a 'while' loop is set to run indefinitely with the condition 'true'. The 'until' loop is introduced as the inverse of the 'while' loop, running until a condition becomes true, demonstrated with a coffee order script.

10:06

πŸ”„ Exploring the Power of For Loops in Bash

The presenter delves into 'for' loops, which iterate over a list of items. A variable 'cups' is used to loop through a list of numbers, demonstrating how the 'for' loop assigns each item in the list to the variable in turn. The script is executed to show the loop printing out the number of cups of coffee. The versatility of 'for' loops is further illustrated by modifying the list to include a range of numbers using curly braces. The paragraph concludes with an example of using 'for' loops to check the connectivity of websites by pinging them within the loop, showcasing the practical applications of 'for' loops in scripting.

15:07

🌐 Practical Applications of For Loops in Networking and Weather Checking

This section presents practical examples of using 'for' loops for network checks and weather updates. A script is shown that pings a list of domains to check their online status, utilizing the 'for' loop to iterate over each domain. The script uses conditional statements to determine if a domain is up or down based on the ping response. Another example script is introduced that uses the 'for' loop to fetch and display the weather for a list of cities stored in a text file, demonstrating how 'for' loops can be used with command output and APIs to perform useful tasks.

20:08

πŸ”„ Loop Control Statements: Break and Continue

The video concludes with an explanation of 'break' and 'continue' statements in loops. The 'break' statement is shown in the context of an infinite 'while' loop that checks if a website or IP is up, exiting the loop once a response is received. The 'continue' statement is demonstrated with an elevator script example, where the loop skips printing the number 13 as per the condition set. The presenter encourages viewers to experiment with these loop constructs and to share their creative scripts on social media or GitHub.

πŸ“’ Encouraging Script Sharing and YouTube Engagement

In the final paragraph, the presenter invites viewers to share their scripts and workout progress on social media or GitHub, expressing interest in seeing what the audience creates. The call to action also includes a reminder to engage with the video on YouTube by liking, subscribing, and turning on notifications to 'hack' the algorithm ethically. The presenter wraps up the session, acknowledging the effort put into the video and looking forward to the next session.

Mindmap

Keywords

πŸ’‘Bash Script

Bash script refers to a scripting language developed by the GNU Project, which is used to automate tasks on Unix-like operating systems such as Linux. In the video, the main theme revolves around building a push-up counter using Bash script to demonstrate how loops and other scripting techniques can automate and simplify tasks, making both coding and physical exercise more efficient.

πŸ’‘Loops

Loops are a fundamental concept in programming that allow a set of instructions to be executed repeatedly until a specified condition is met. In the script, loops are used to count push-ups, read through files line by line, and perform actions multiple times. They are essential for automating repetitive tasks and are a key focus in the video.

πŸ’‘While Loop

A while loop is a type of loop that repeatedly executes a block of code as long as a specified condition remains true. In the video, the while loop is used to create a push-up counter that continues to prompt the user for push-up counts until a certain condition is met, demonstrating the power of loops in scripting.

πŸ’‘Until Loop

An until loop is similar to a while loop but runs until a condition is true, rather than while the condition is true. It's the 'upside down' version of a while loop. The video uses the until loop to illustrate how it can be used to wait for a condition to be met, such as waiting for a user to type 'coffee' to exit the loop.

πŸ’‘For Loop

A for loop is used to iterate over a sequence of values. In the video, the for loop is demonstrated with a 'to do' list and later with a range of numbers. It is also shown iterating over a list of website domains to check their connectivity, showcasing the versatility and power of for loops in executing repetitive tasks with different sets of data.

πŸ’‘Break

The break statement in a loop is used to exit the loop prematurely, regardless of the loop's condition. In the script, 'break' is used to stop a potentially infinite while loop when a certain condition is met, such as detecting that a website is up and running. This demonstrates how 'break' can be used to control the flow of a program effectively.

πŸ’‘Continue

The continue statement is used to skip the current iteration of a loop and continue with the next iteration. In the video, 'continue' is used in the context of an elevator example to skip a particular floor (number 13), illustrating how 'continue' can be used to control the flow inside loops and handle special cases.

πŸ’‘User Input

User input is a method of receiving data from the user that allows for interactivity within a script. In the push-up counter example, user input is used to gather the number of push-ups performed by the user, making the script interactive and personalized to the user's actions.

πŸ’‘Virtual Machine

A virtual machine is a software emulation of a computer system. In the video, the use of a virtual machine like Linode is recommended for setting up the lab environment to write and test Bash scripts. This provides a platform that is 'fast' and cost-effective for learning and experimenting with scripting.

πŸ’‘Ping

Ping is a network administration tool used to test the reachability of a host on an Internet Protocol (IP) network. In the script examples, ping is utilized to check if a website or an IP address is up and responding. This is a practical application of networking commands within a Bash script to perform real-world checks.

πŸ’‘Conditional Statements

Conditional statements are used to perform different actions based on different conditions. In the video, if statements are used within loops to check for certain conditions, like whether a ping request receives a response, and then to decide whether to continue looping or break out of it.

Highlights

Building a push-up counter in bash to enhance both coding and physical fitness.

Introduction to while loops for creating scripts that repeat.

The use of until loops as an alternative to while loops.

A practical example of a push-up counter using while loops.

Utilizing read command for user input within bash scripts.

The potential dangers of infinite loops with 'while true'.

Exploring the until loop with a coffee ordering example.

Introduction to for loops for iterating over lists.

Creating a script to check website connectivity using for loops.

Using for loops to iterate over the output of a command.

A demonstration of checking multiple websites' up status with a bash script.

Using break to exit a loop when a condition is met.

The use of continue to skip certain iterations in a loop.

Practical applications of loops in real-world business contexts.

Invitation to share custom scripts and workout progress with the community.

Encouragement to ethically 'hack' the YouTube algorithm for better visibility.

Transcripts

play00:00

This bash script is going to make you.

play00:03

Root.

play00:03

Because in this video, we're gonna build a push up counter in bash,

play00:07

building up your bash skills and your muscles at the same time. Hi, got time.

play00:11

Watch out. Ladies

play00:25

Loops allow us to write scripts that repeat loops.

play00:28

Allow us to write scripts that repeat loops.

play00:30

Allow us to write scripts that re whoa got caught in a loop there.

play00:33

Sorry about that. Welcome to bash episode five,

play00:36

where we are going to build up our bash muscles and our real muscles with

play00:40

something called loops. We're gonna start off with wild loops.

play00:43

We'll actually use these to build our pushup counter.

play00:45

Then we'll move on to until loops.

play00:47

And then this one you don't wanna miss four loops. It's my stink and favorite.

play00:51

I love these things. Welcome to can Chuck beat the machine? Hold on,

play00:54

need more lights. Can I do 50 pushups before Le node deploys,

play00:58

this spiritual machine ready set, kind of nervous. Go.

play01:04

Now to fall along with this lab,

play01:06

you will need a virtual machine like Lennox from Le node.

play01:12

I use Le node because they're so fricking fast.

play01:15

Can I do this is still provisioning. Okay. Wicked fast.

play01:18

And I deploy a virtual machine on N every time I have to do a lab.

play01:23

Holy crap, because it cost almost nothing. $5 a month.

play01:29

And the only charge you 0.0, zero, like 75 cents per hour. I'm dying.

play01:35

Did I lose already? Oh crap. I got lost.

play01:39

How may I get done? Video editor. Oh, freaking a okay. Ode one.

play01:43

But like I said, this lab will only cost you 0.0 75 cents an hour.

play01:48

Use it for an hour, delete it.

play01:49

And it only costs you a penny or it can literally cost you nothing.

play01:52

If you head over to leno.com/network, Chuck, I need some coffee.

play01:57

If you're new to Leno, you'll get a hundred dollars credit for 60 days.

play02:00

Just do it. It's awesome.

play02:02

And all the scripts I'm gonna show you in this video are inside of stack script.

play02:05

And all you have to do is go find it. Just search network.

play02:07

Chuck I'll also have that as a link below,

play02:09

click on that sucker and select deploy. That's it. After you put in some info.

play02:13

So go ahead and get your lab going.

play02:15

I'm gonna take coffee break and then we'll,

play02:18

we'll get started with bash episode five. So once your Le node is booted,

play02:22

it's running like this.

play02:23

We'll go ahead and jump into its terminal by accessing it via SSH.

play02:27

I'm gonna copy this command right here.

play02:28

Copy launch my command prompt or terminal for windows or Mac.

play02:32

I mean Lenox or Mac I'm I'm so tired right now.

play02:34

Pace that command and let's get started except I fingerprints and put in the

play02:38

password We're in now real quick. If you type in LS,

play02:42

you should see how the fun scripts you're gonna play with there. They all are.

play02:45

Yes. You don't even know what's coming. Not no time to waste.

play02:48

Let's go ahead and write our push up script. We'll start a clean script.

play02:50

Nano pushups.sh hit enter.

play02:54

And then Ang did I scare you? I'm gonna get you one of these times,

play02:57

put in your shebang at the top. If you're like,

play03:00

what the junk is that go back and watch the first four episodes in the series

play03:03

you'll be caught up. So we're gonna start with the wild loop.

play03:06

The wild loop will loop or repeat your script over and over.

play03:09

As long as something is true,

play03:11

let's write one real quick and I'll show you what it does. It's pretty cool.

play03:13

Let's first create a variable. We'll call it X and we'll have it equal.

play03:16

The integer one equals one, and then we'll start our loop.

play03:20

So it'll actually start with Y if you spell it right, Y there we go.

play03:25

And then right after Y will be our condition.

play03:27

So I'll try this and type it with me. We'll do opening brackets,

play03:30

two of them space. Then we'll call our variable X dollar sign X.

play03:35

And we'll say, this has to be less than or equal to 100.

play03:39

We'll do a space closing double brackets. And that's our condition.

play03:43

This sucker will evaluate the true. So while this sucker is true,

play03:47

do something, and we're about to tell it to do something right now.

play03:50

So I'll go ahead and hit, enter, and then type in do,

play03:53

keeping in mind that as I'm doing this,

play03:54

I'm creating space to make it look clean and nice. So you don't have to do this.

play03:58

And you may see scripts that are just kind of jumbled together,

play04:00

kind of hard to read, but we're gonna keep it pretty. So type in do,

play04:03

and then hit enter once more and let's tell it to do something.

play04:06

I'm gonna hit a couple spaces again, to make it pretty. And I'll say echo,

play04:09

and we'll make the echo say this. Hey,

play04:12

I just did the variable X

play04:16

pushups and close that string out.

play04:18

And then we'll do one more kind of weird thing right after the echo.

play04:21

Don't worry. It's not too strange, but just type it with me real quick,

play04:24

we're gonna do opening parentheses two of them space.

play04:28

Then we'll type in X and then space plus plus space and closing

play04:32

parentheses.

play04:33

Now all this little weird thing is doing is simply adding one to our X,

play04:37

our variable X plus one, and it's gonna do something pretty cool. Watch this.

play04:42

Okay, let's go ahead and close this out. We'll hit enter,

play04:45

and then to close out our loop to say, we're done. We'll just, we'll tell it.

play04:48

We're done type in done.

play04:50

So remember this key thing right here with our wild loops and which with every

play04:53

loop we're gonna do here in this video,

play04:55

what's going to be looped is between the do and the done.

play04:59

And you must put those to close it out. So what do you say? We try it.

play05:02

You're about to get ripped. You ready? So say control X Y enter to save.

play05:07

We'll make our script executable real quick. We'll do ch mod plus X,

play05:11

and then the name of our script, pushups.sh.

play05:14

And then we'll run our script with period slash pushups.sh you ready for this?

play05:20

Hit enter and go, dude, how'd you do so many pushups,

play05:23

easiest pushups you've ever done. Take a little coffee break.

play05:26

You did a great job. If you go through and count, you did a hundred pushups,

play05:30

congrats, but you're still weak.

play05:32

Wey, Wey, Wey you a little beat at work.

play05:36

Because yeah, automation's cool. Except when it comes to your health,

play05:39

you probably shouldn't automate your health.

play05:40

Let's get back into our script and fix that nano pushups.sh.

play05:44

So what did we see there?

play05:44

We saw that we started with our variable X equaling one and with our wild loop,

play05:48

as long as our X variable was less than or equal to a hundred,

play05:51

this loop is gonna go on forever,

play05:54

which obviously it didn't because inside the loop,

play05:55

we incremented the variable variable X by one, each time it was run,

play05:59

but you're lazy and weak. Take out the echo, take that sucker out.

play06:02

Now let's change that to read. And if you recall reading,

play06:06

it's how we get user input from our script.

play06:08

And the user in this case is gonna be you.

play06:10

You're gonna be inputting some work here.

play06:11

We'll type in dash P and they'll have a say something. We'll say push up,

play06:16

we'll call our variable X, and we'll say, press enter to continue.

play06:21

And then right at the end, after the done,

play06:23

we'll give a little congratulatory message. Now,

play06:25

all we've done here is required user input.

play06:28

Every time the bash loops and tell you what one hundred's way too much.

play06:31

Let's lower that a bit. Just so the sake of our example,

play06:33

we'll do 10 and that should be good. So let's do control X, Y enter to save,

play06:37

and let's try out our script real quick now,

play06:39

because I don't wanna get down on the ground again.

play06:40

I'm just gonna do some squats. You can do anything you want.

play06:42

Let's run our script. Three fours. Let's push up. Sh, here we go.

play06:46

Push up one squat one, enter squat.

play06:49

Two enter.

play06:55

Okay. Oh yeah. Coffee break. I hope you did that with me, by the way. Hey,

play06:59

there, ain't nothing wrong with getting a little workout while you're learning.

play07:02

Get that brain pump the gave it talk right now.

play07:04

Get the blood in your brain pumping.

play07:06

Now you probably won't see too many wild loops forcing you to exercise.

play07:09

So this was a weird use case,

play07:10

but a more practical use case might be something like this.

play07:13

Go ahead and open up. One of the example, files read underscore example,

play07:17

sh this one will look a bit weird, but what this will do is open up a file.

play07:21

And in this case, the file is called Hamlet.

play07:23

It's literally the entire Shakespeare play Hamlet in one text file.

play07:26

And this wild loop will literally go through each line line by line and echo it

play07:30

out. And as long as there's a line to read,

play07:32

which is what that condition is saying, it'll go through it all.

play07:35

And if I run it, I'll show you what happens.

play07:36

Bash read example to sh did it.

play07:39

Who 5,566 lines. Now let me warn you.

play07:43

Don't you mess around with wild loops. They can be dangerous.

play07:46

Let me show you let's start a new script real quick,

play07:48

nano dangerous.sh watch out shebang.

play07:52

Got I got you type in wild and the right after

play07:57

while type in true. This right here is scary. Are you scared?

play08:01

Let me show you why you should be scared. Let's try it out. Hit enter type in,

play08:04

do enter a couple spaces. Say echo, are you scared?

play08:07

enter type in done control X Y enter to save.

play08:11

Now let's run that sucker bash dangerous to sh you ready for this?

play08:15

It's going on forever. What just happened there? Well,

play08:18

let's jump back in there real quick. Remember,

play08:19

the wild loop will run as long as they condition is true. And when you say wild,

play08:23

true, you're basically saying while true is true. it's, it's like, well,

play08:27

true. True. Well, that's always gonna be true. It's always going to run.

play08:29

People do use this. So be careful, but control C will always save the day.

play08:33

Now it's time for the upside down version of the wild loop. The until loop.

play08:37

It's the exact same thing. Just backwards, upside down. That's all it is.

play08:41

Let me show you let's open another example, script real quick,

play08:44

nano until example one sh notice it does look pretty

play08:49

familiar.

play08:50

The main difference being instead of while we have until now a while loop will

play08:54

run while something is true and until loop will run until something is

play08:59

true. So look at our condition real quick.

play09:00

We're saying until the variable order equals the string coffee,

play09:05

we want you to loop. So this right now, currently isn't true.

play09:10

And this will loop until it becomes true.

play09:12

And the look here what's happening in the loop. It's gonna echo,

play09:14

would you like coffee or tea? And then we're going to read, get customer input,

play09:18

get input from you and whatever you type in,

play09:20

we'll be put into the variable order. So I'll ask you, Hey,

play09:22

do you want coffee or tea? As long as your order,

play09:24

the variable order doesn't equal coffee. This single will loop.

play09:27

Let's try it out. Control X, get outta there.

play09:29

We'll run it with bash until example, sh would you like coffee or tea?

play09:33

Let's type in tea? Well, that didn't work because we didn't type in coffee.

play09:36

Let's try juice now. Milk. No,

play09:40

but if we type in coffee, ah, finally, excellent choice. Here is your coffee.

play09:45

That's the only right answer. So jumping in there real quick,

play09:47

once more until as just the upside down of while now, time for the fun stuff,

play09:52

four loops these things you can get lost in let's hours, playing with it,

play09:56

looping with it. Don't get yourself caught in a four loop.

play09:59

let's try it out though. Let's go ahead and write a new script.

play10:01

So nano we'll call this four HBE, sorry. Now,

play10:05

to start a four loop, you're probably guessed it.

play10:07

We're just gonna start typing in four, instead of until, or while the four loop.

play10:11

He's not lazy. He's not gonna wait for something to be true or false.

play10:14

He wants a list of things to do. So let's give him a to do list real quick.

play10:17

Let me show you so right after four, let's create a variable.

play10:19

Let's just call it cups. And then we'll say N and this is gonna be kinda weird.

play10:24

One space, two space,

play10:26

3, 4, 5, 6, 7, 8, 9, 10, and then semicolon at the end.

play10:31

What is happening? cause I know it was kinda weird.

play10:33

And if this does feel weird, don't feel bad. This took a bit for me to get it.

play10:36

When I first learned it,

play10:37

what're saying here is our variable cups will become each of the items in this

play10:41

list. That's what it's gonna do.

play10:42

And it will loop through the script as each variable.

play10:45

So for one loop cups will equal one for the next loop.

play10:49

It'll equal 2, 3, 4,

play10:52

and it will loop through the entire list until it gets to 10 and in the loop is

play10:57

done. So let's do something it'll make more sense when we do stuff.

play10:59

So just after the semicolon,

play11:01

let's hit enter just like our while and until loops will type in, do hit enter,

play11:04

give it a bit of space and we'll say echo, Hey, you've had,

play11:09

and let's go ahead and call that variable cups, dollar sign cups.

play11:12

And then we'll say, cups of coffee today.

play11:16

Then we'll hit enter. And then at the end of our loop, just like while,

play11:19

until we'll type in done. So when we run this,

play11:22

this variable cups will become each of the items in this list.

play11:26

It'll do it 10 times. Let's try it out. Hey,

play11:28

control X Y enter to save and we'll do bash four.sh to run our

play11:33

script. Isn't that cool. Look at that.

play11:35

Each time the cups variable changed his identity based on our list in one loop,

play11:40

he was one in another loop.

play11:41

He was seven and that's the power of four loops is kind of crazy.

play11:45

Now in just a moment, I wanna show you a four loop.

play11:47

That'll allow us to check the connectivity of different websites.

play11:50

It's very cool. And it shows the power of four loops.

play11:53

Lemme show you one thing real quick.

play11:54

Let's jump back into our four sh file and let's go to the top to where our

play11:58

numbers are. Our 1, 2, 3, 4, 5. You know the list. Let's take it out.

play12:01

I wanna show you a different way. We can list these numbers.

play12:04

We can do a range instead of just listing them individually,

play12:07

by doing an opening curly brace typing in the first number,

play12:10

doing a.dot and then the last number, and then closing curly brace.

play12:15

And the reason I wanted to show you that is because you'll probably see it in

play12:17

the wild and go what's going on there. So I want you to be aware.

play12:20

So if we save this, so X Y enter to save, run the script once more,

play12:24

does the same thing, not time for the internet thing,

play12:26

we're gonna check and see if certain websites are up with the, for loop.

play12:29

And then I'll show you one with the weather like it's it's nuts.

play12:31

Four loops are endlessly fun. So go ahead and open another example file.

play12:34

We'll do nano four underscore loop, ping domains. St.

play12:39

It looks just like this. Go ahead and open that file right now.

play12:41

Now this one's pretty cool.

play12:42

And I hope it gets the gears kind of turning in your brain of like,

play12:45

what else you can do with this. But here's, what's happening.

play12:47

Notice we're starting the same way.

play12:48

We have our four and we're calling instead of cups, we're doing the variable X,

play12:52

and we're saying four X in a list of websites.

play12:56

And remember what it does here. Every time this loop will run,

play13:00

X will become a different thing in that list.

play13:03

And one loop X will become Google and the next loop X will become B.

play13:07

And in that loop here is what's happening and notice we've done something kinda

play13:11

weird. We have an if statement, a conditional,

play13:14

we're combining the knowledge of episode four and five. What?

play13:17

And it's not too crazy. What it's doing, all it's doing is saying, Hey, if,

play13:20

and then we're gonna ping it. It's gonna ping with a few options.

play13:22

Nothing too crazy. We're just saying, Hey, make it quiet.

play13:24

Let's just do two pings.

play13:26

And we're only gonna wait for like one second to receive those pings.

play13:29

And then what we're pinging is the variable X,

play13:31

which remember X will be Google or ping or Facebook or network.

play13:34

Chuck it'll be all of those as it goes through.

play13:36

And then all we're saying right here is we want the ping output to not be there.

play13:39

We don't wanna see the individual responses when you ping something.

play13:41

And what's cool about this one statement. The, if ping,

play13:44

just this guy right here,

play13:45

all it's saying is if we receive a response back when we ping something,

play13:49

it's true. So if it is true, then go ahead and do this action.

play13:53

We say, then do this. If we don't receive a ping back,

play13:58

then it will do our else statement.

play14:00

And basically we're gonna say the domain is up or the domain is down.

play14:04

It'll go through our list and then be done. Let's try it out real quick.

play14:07

Control X, get outta there. We'll do bash four loop, ping domains.

play14:17

Bam. Isn't that cool. And then I went ahead and added a host.

play14:20

I know it was down. And then bam, look at that. Try to ping it. It's down.

play14:23

So you could add a list of your favorite websites or maybe your company's

play14:26

websites and just kind of do a quick little,

play14:28

let's see if they're up just for fun. And then here's a really fun one.

play14:31

Go ahead and open up for_weather.sh

play14:36

this one, I love this one. Look at our for loop. We don't have a list there.

play14:40

Instead we have a command yes. We have a command.

play14:45

That's opening up a file. What? yes, you can do that.

play14:49

I told you for loops are crazy. So actually let's, let's back out real quick.

play14:52

I'm getting too excited. Let's back out. Let's run that command.

play14:55

Let's cat cities dot TXT. This is a file that should be there.

play14:58

If you launched my stack script in Le node inside cities dot TXT is a list of

play15:03

cities shocker, right? So when that four loop runs that command,

play15:06

it's going be looking at this list. So when I say for X,

play15:11

N blah, blah, blah, X will become each of these cities in that file.

play15:15

How cool is that? All right, anyways, let's get to our file. Open it once more,

play15:19

nano four weather sh.

play15:21

And what this will do is something that we've used in a previous episode in the

play15:24

series, it'll use the WTT R dot N um, curl interface.

play15:28

We can actually curl the current weather for a city,

play15:31

getting the weather and your terminal, which is just really nerdy and awesome.

play15:34

So here's what we're doing for X, our variable in cities.

play15:38

So X will become each city in that text file. It'll run this command,

play15:43

noticing that inside of our URL, we have the variable X right there,

play15:47

and then it'll echo out the weather for that city. Let's try it out real quick.

play15:51

Bash four weather.sh. Here we go. Bam.

play15:55

Look at that.

play15:56

A quick weather forecast of all the cities you could possibly imagine just like

play16:00

that. Now this is silly fun,

play16:02

but I hope it kind of paints the picture of what a four loop can do.

play16:06

And frankly, while loops and until loops, they are all extremely powerful.

play16:10

So yeah. Have fun with it. Practice dump, stuff like this,

play16:13

because the things you're doing here can be used in a real world business

play16:17

context. Trust me. It's awesome. Now real quick.

play16:20

I wanna loop back to wild loops cause I wanna show you two concepts that really

play16:24

apply to all these loops, but it'll be our break and our continue.

play16:28

These are two concepts we can use inside of our loops to change the behavior.

play16:32

And remember earlier I said that using a wild loop with the wild true can be

play16:37

very dangerous. It can be,

play16:38

but using with break is actually something people do like watch this.

play16:42

I have a little script here that will check the up status.

play16:44

Like is a site up or is an IP address up with the wild loop? Let's try it out.

play16:48

So let's do nano for internet and internet down.

play16:52

Toch it's actually a wild loop. I don't know why I call it four,

play16:54

but just ignore that. And here we go. Pretty simple opening. It's gonna ask you,

play16:58

Hey, what website or IP do you wanna check?

play17:00

It's gonna ask you for it in store it in the target variable and then whew,

play17:04

here's that dangerous statement? Wild. True. This will run forever.

play17:09

Run forever unless you stop it or unless we break it.

play17:13

That's what happens here. Notice we have an,

play17:15

if conditional here and we have that same ping that we did from earlier.

play17:19

So if what we ping does respond,

play17:22

that's automatically true and it will do this. It'll say, Hey,

play17:25

you're up and then break. Get outta there. Bust out that script.

play17:30

The loop is done. That's what break does otherwise or else it'll tell you, Hey,

play17:34

it's down and it will keep looping notice. I have a sleep two.

play17:37

It'll wait two seconds, then loop again.

play17:39

Then loop again until that one thing is up.

play17:41

So I actually have another server in Le node that is certainly down right now.

play17:44

Let's test it out with that. So let's bash four internets down,

play17:49

sh run that sucker. What do I wanna check?

play17:51

I'm gonna throw in this IP address and let it run. It's currently down.

play17:54

And it's gonna tell me that until it's up, which I think is very cool.

play17:58

So I don't know about you, but sometimes I'll be rebooting my things,

play18:00

whether it's an internal IP address or something,

play18:03

external could be a domain or whatever. And I'm like, okay,

play18:05

I'm gonna reboot it way for it to come up.

play18:06

And this is kind of a cleaner way to do this versus just having a constant,

play18:10

continuous ping. So I'm gonna go ahead and boot it up real quick.

play18:12

I'm not gonna make you wait, just take a sip of coffee and you'll see it in.

play18:15

Like, I dunno, you'll see it now. So,

play18:17

whereas a continuous ping would just go on forever. Uh,

play18:20

this stops as soon as it got an up response,

play18:25

it broke out of the script break,

play18:27

which that's super useful and handy now for the next one.

play18:30

Continue this one's cool. And I love the example that Sean Powers used.

play18:34

So thank you Sean for this. And it's an example of an elevator.

play18:37

So go ahead and open up the next script.

play18:39

Nano elevator example.sh here we have another four loop and it should look

play18:43

very, very familiar. We're doing a simple couple echoes here at the beginning.

play18:47

We're looping through a sequence of numbers that we're defining right here,

play18:51

but then we have something curious here. We have an, if conditional,

play18:55

if the variable X equals 13, then continue. What does that mean?

play19:00

It means we're going to skip that loop.

play19:02

SOAs break will just break out and be done.

play19:05

Continue is just a simple skip.

play19:07

It'll skip that loop and move on to the next loop, which is super handy.

play19:11

Let's try it out real quick. Bash elevator example,

play19:14

the sh welcome to the hotel. We're going up floor one floor two,

play19:18

and video editor. Go ahead and speed up a little bit until we get to 11, 12,

play19:23

it's coming up watch. So 10, 11, 12, 12,

play19:29

no 13. You see in our condition, we said, if it equals 13, continue,

play19:34

skip that loop. And it did that. Get your before and after pictures in order,

play19:39

you're about to get ripped from your wild loop.

play19:42

I hope you do build that out and just work out with it. It's not a bad idea.

play19:46

Also, if you're doing it like a computer or a laptop and get that isometric, I,

play19:49

I can't even say it. Isometric thing going on, try it out. Be a nerd.

play19:52

It's awesome. And get ripped in the process. But anyways, while until,

play19:56

and four loops are essential bash, scripting,

play19:58

things that you're gonna use all the time,

play20:00

they truly do unlock the power of bash scripting and how you can just do things

play20:04

faster, better. And they're simply just, they're fun.

play20:07

they're really fun. So go crazy with it. In fact,

play20:10

I would love to see your scripts. So if you do something really cool, fun,

play20:13

inventive, or just something dumb that you wanna show me, I wanna see it.

play20:16

Let me see it. Show me on Twitter, tag me or something. Send me your GitHub.

play20:19

I wanna see your scripts. And if the post ups work out, man,

play20:22

send me some after photos. I wanna see those guns. Yeah.

play20:24

I would like to take it to the gun show. So anyways, that's all I had today.

play20:27

Thanks for having some coffee with me and talking about some bash scripting.

play20:31

Oh wait, almost forgot. Have you hacked the YouTube algorithm today?

play20:34

Let's make sure you do hit that like button notification, but comment.

play20:37

Subscribe. You gotta hack YouTube today. Ethically of course.

play20:42

And yeah, I've been recording too long and I'm sore and I'm tired.

play20:45

This one took a lot outta me. So you're welcome.

play20:50

Get you guys next time.

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Bash ScriptingLoopsCoding TutorialFitnessPush-up CounterAutomationVirtual MachineLinuxProgrammingTech Education