Change language

BASH scripting will change your life

BASH scripting will change your life

We already know that bash is powerful and it will make you an awesome it engineer, but it can also change your life. Well, kind of it can actually make each day of your life better. So in this video, allow me to introduce the bash Butler, a script designed to make you feel awesome and informed every day. In fact, heres a sneak peek. Whoa, whoa, hold on, buddy.

You gotta watch the video first because there are things in there that might be new to you. Like variables and arguments, really fun stuff that youre gonna learn in this video. So my bash friends, my bash club, my bash brothers and sisters, thats probably better hit your coffee. Ready? Lets dive deeper into bash.

The first thing were gonna do is actually start writing a script with the knowledge we gained in the first episode and see if you can do this by yourself.

Heres the criteria, name, your script best day

And I want your script to greet you with these three messages and feel free to replace Chuck with your name and bonus.

Give one set of waiting time between these two messages and finally make your script executable. Pause the video. Go UN pause.

Lets walk through it real quick. And by the way, if you have no idea how to do this, thats fine.

Just go back and watch episode one.

I show you how to do this and youll need that knowledge in order for us to continue building this crazy awesome script. Just trust me. Here we go.

Now first Ill spin up my Linux lab by going to in the

Chuck here in ode, Ill actually create a virtual machine, a Linux virtual machine in the cloud, and it will only cost me one penny per hour, which is insane.

And if youre neutral ode, you get a hundred dollars free credit for 60 days just for signing up.

And as you can probably see, ode is my cloud provider of choice.

I have like a million virtual machines in the cloud. Its how I run my business.

And its my favorite play used to practice Lennox because I can quickly spin up a Lenox lab in the cloud. It boots up in seconds and when Im done, Ill delete it. So it literally only costs me one penny.

So if you wanna learn Lennox the best way you gotta get hands on.

So go and get logged in below. We, Chuck, and look, mines already up and running.

So Im gonna grab my SSH access over here just by copying this one little command and Ill launch my command prompts in windows or terminal and Mac and Lennox, pace. My command hit enter, accept all fingerprints, put in my password and Im it. And by the way, if you already have your own Linnux machine, that works too. So here we go.

Time to write a script, quick coffee break. Dont worry.

Itll be plenty of these. And I will go a bit fast. If you are confused, go back to epic. Oh one, here we go. First, lets create a script named best day lets do it nano best day ever. DOH done. Im already editing a script.

And how do we start each script Shang? Sorry.

Didnt mean to scare you shebang in place, Rockton task. Number two, lets knock it out.

Im gonna use the echo command to talk to myself or have my script.

Talk to me and Ill use a sleep command to make it sleep in between commands Looks good, task two, done task. Number three, lets make the sucker executable control X Y enter to save.

And then Ill do C H mod plus X and the name of my script.

Bam. We did it. If you did that by yourself. Thats awesome.

And what do you say? We actually run our script, Man. I already feel so great. Best way to start the day. Dont you feel awesome, but theres more. We can do more, but hold on. Before we do that, this has awesome and really great for us. You Chuck, whoever you are, but what if our friend Patricia, what if she also wants to start her day? Awesome. And honestly, she needs this cuz she cant even get into a coffee shop.

So let me ask you, what do we have to do to make sure that this says Patricia so that she has a great day? Well, you might think lets just go edit the script and we could do that.

Well type in nano once more, the name of our script, best day ever dots sh and edit that sucker.

And we could painstakingly go through and change each of these names, but we cant do that. Were it people and we are lazy.

So I wish there was a way we could just change all the names at once. There is.

So thats why were doing this right there is its really cool. Check this up new concept time. Were going to create something called a variable.

Go ahead for me and type in name. Thats it. And then do the equal sign.

And then in quotations type in lets just say Patricia and then close with quotes. There we go. This right here, this name, that sucker is a variable.

And when you think of variable, I want you to think, Hey, it can change.

And heres what were gonna do with it.

Ho ho instead of manually typing in Chuck or Patricia or Ben evil Ben, instead of doing that, well go in here and Ill use my Curia.

Go ahead and do this with me right now.

Im gonna take away Chuck and Im gonna type in dollar sign name, and Ill do it on every one of these lines here, remove all the chucks.

Now well walk through this, but I wanna just execute it real quick.

Lets do it together. So well go ahead and exit and save our file.

Control X Y enter and lets to run our script period four slash best day ever.

And check this out. How cool is that? It replaced the name that we put in there with a dollar sign with Patricia, lets jump back in the script nano best day by simply typing in the word name and saying, Hey, I want this to equal something.

And this example is just gonna be text that says, Patricia, we created a variable. And then we use that variable by using a dollar sign.

And then the name of that variable, which its also name, which is kind of confusing. Its okay. We well get past it. So again, here we created the variable and here we used it. Lets try it again real quick.

Instead of Patricia, lets change the variable name to Ben.

Go ahead and do that for me real quick. Pause. Im pause coffee break.

Now lets do it. Ill change Patricia to evil.

Ben control X Y enter lets run the script one more time, best day ever for evil. Ben, how cool is that? We only had to change one thing in our script and it changed everything else.

Thats a power of variables and not only do they save us some time, thats awesome. They are also cool, crazy powerful.

And youre about to see that.

Now lets jump back in our script nano best day ever do sh what we did here was cool. We manually set a variable name equals Eagle bin or Patricia, whoever it is, but thats not the only way we can set variables.

And thats a good thing because at this point we still have to manually change something. And again, were lazy. We have to automate this.

Thats what bass scriptings all about.

So what I wanna do is change it to where whoever comes in and uses my best day ever script. Itll just match to them. For example, if my wife Abby comes in, I wanted to just say Abby or my daughter, Chloe, or my brother Cameron, or my video editor, Nick. I want this script to make their best day ever happen.

And I dont want to have to do anything. How do we accomplish that two ways? Check this out first way. Go ahead and delete this right here. Take it out.

Goodbye. Goodbye, April Ben. And this might feel a little weird, but go ahead and type this in read space name. Okay. What does that mean ? Ill tell you in a second before that, lets go ahead and do an echo and say, what is your name? You probably see where this is going, but lets, Im not gonna spoil it for you.

Lets take control X Y enter to save and get outta there and lets run our script best day ever at sh run it. What is your name? Okay.

Maybe we should answer. Lets say Nick, What, how cool is that? We just got even lazier mission accomplished.

Lets get back into our script nano best day ever.

Sh what just stinking happened instead of setting the variable name manually just saying name equals Patricia name equals Ben. Were saying, Hey, why dont we ask them what the variable should be? Lets ask the user, the one using the script. And thats what the read over here is doing.

Read will get some user input and that input will be stored inside the name variable or the variable name. Thats amazing, right? Thats so fun.

Think of all the things you could do with this. In fact, you should play around with this. Just have fun. Go crazy. Have your, your wife, your husband, your brother, your sister, your friend, your coworker, run the script and do something. I set a lot of names there.

Ive had a little bit of coffee this morning.

Now there is one more way to do this, and its really fun. Check us out.

So go ahead and take out Rena. In fact, take echo. What is your name? Take out all that. And were gonna do this. And this is gonna feel weird too.

Really weird type in name like we did before.

Like when we were setting the variable manually type in equals, but this time do this with me. Dollar sign one. What, what is that? Im not gonna tell you. Were gonna run the script control X Y enter to save.

Im gonna do control. Will L to clear my screen, make it nice and clean, and were gonna run the script again, but hang with me real quick.

Go and type in period slash best day ever.

Sh but before we run it right after that, go ahead and put in a name.

Ill see my wife, Abby. So Abby and go, what whats happening? How did that get there? Right there? We just got into an, an argument, a really fun argument. Thats what these are call by the way.

Lets get back into our script, nano best ever. Ah, Ive been saying that too many times.

We just got into an argument with our script and we actually won and had fun specifically. This is called a positional argument or a positional parameter.

And whenever we ran our script, you know, period, four slash best a ever SSH, whatever parameter we put right here after the script, well match up $2, sign one keeping in mind that its the first parameter were putting.

So it matches up to one first one, first one. Got it. Yes. Okay.

And that mean s we can do this, check it out. We can have more parameters.

For example, my wife, Abby probably doesnt wanna be complimented on her beard.

Just a wild guess. Probably wont make her best day ever.

So we gotta change that. So real quick, actually challenge knowing what weve done here, use a positional parameter and argument to make that change and make you unique to everyone who comes in. Pause a video UN pause, coffee. Purple.

So first Im gonna set a new variable. Now Im gonna use a positional parameter.

Its kind of fun. Say positional parameter.

I wanna name mine compliments and Ill have that equal dollar sign two.

And then down here under beard, Ill just put in the variable dollar sign complement. Bam. That should do it.

Control X Y enter to save. Lets try it out. So lets run our script period.

Four slash best day ever dot H Ill say Abby as the name.

And then my second parameter will go right after that. And Ill say eyes, lets try it out. This is so cool. Right? Bam. Good morning, Abby, look at that.

We see your first parameter in action. Abby, Abby, Abby, and then our second one comes in right here. Eyes instead of beard.

Thats just fun. Thats just so cool. But the fun isnt over yet, our bash Butler best day ever, a script isnt done, he can be and do so much more. So using the skills weve learned so far, lets take it to the next level. Lets go. Lets jump back into our script.

Nano best day so far, weve set variables equal to simple things like text or strings.

Thats what we call text in the scripting world. But this is crazy powerful.

You can also have variables equal the of commands. What does that mean ? Let me show you. So actually lets get out of our script.

Sorry I made you do this. Needlessly hit control X.

Lets learn a few quick commands real quick to figure out who youre logged in as like, who am I type in one word who am I? Thats the command. So fun.

You are root. Youre logged in as root. If you set up Le node, you might be somebody else. That is your username.

Another fun one is to figure out, Hey, where am I in the file system? Am I in my home directory? Am I on the desktop? Am I in downloads? We can figure that out real quick with the P WD command print, working directory, bam. Hey, were in the root directory. Cool.

And one more fun one if youre like, Hey, what day is it today? Just type in date. The command is date bam and tells you.

So now what were gonna do is take the output of these commands and put them inside a variable that we can use in our script. Lets try it out. Okay.

Lets go.

Lets jump back into our script nano best day ever about sh first thing well do is set our variables. So up here, just under compliment, well create a variable called user and well have that equal.

The who am I command? And its a bit different than how weve done it before.

Well use the dollar sign here. I know this is kind of new.

Well do pro parenthesis, open parenthesis and the command who am I? And then close that parenthesis. This is the format well use.

We want to make sure a variable will equal the output of command.

So go ahead and do that for the date command and the PWD command.

Do it right now. Pause the video On pause, coffee break, and lets do it.

Ill create able date and have that equal the command date. Easy enough.

Now Ill create the variable. Where am I? And have it equal to the command.

P w D cool. Now time to use our variables in our script, and you can do this yourself. It could be whatever you wanted to do.

Just something fun. I wanna say down here, sleep two, sleep for two seconds.

And Ill echo, you are currently logged in as, and well use our first variable dollar sign user.

And you are in the directory dollar sign. Ill say, where am I? Theyll say? Yeah. Also today is, and then Ill use my variable date.

Theyll use a quote at the end here to close that up.

And right now thats a pretty heavy sentence, right? Like were echoing a lot of things using our user variable. Our, where am I variable and our date variable. What do you say? We try it out.

Lets do it. Control X Y enter to save. Im gonna do control L pre screen, and then well run our variable keeping in mind.

We have to use our positional parameters too. Read slash best day ever.

Ill say Bob and ice cream. Lets try it out. Ready, set, go. And then yes, the commands.

We are harnessing the power of the commands and using them in our script and variables. Were currently logged in as the root user.

Were in the root directory and is this at a certain time? Whatever.

So not only is our script just encouraging the heck out of us, making us have a great day from the get go. Its also informing us about things.

Speaking of which we can do more.

Like we can go down a rabbit hole and get a little crazy. Do you ever do that? Thats what I do then thats what were gonna do for the rest of this video.

Were gonna go down a little crazy rabbit hole and make this a little extra.

Is that okay? Lets do it. You know, actually were gonna stop there because the videos already too long, about 14 minutes, but dont worry. I still do go down that crazy rabbit hole.

In fact, this is the end result of the script.

Its kind of crazy when you run the script.

In addition to what we already covered, its gonna tell you your private IP address, your public IP, address, the weather of a city of your choosing and the dad joke of the day.

Thats how you have a great day. If you wanna check it out, I do have a link alone, the description to network Chuck academy, that video will live there anyways, back to Chuck in the past to close out this video, back to you, Chuck, and also have you hacked the YouTube algorithm today.

Lets make sure you do hit that light, but notification bell COMT subscribe the, say it all. Yes. You gotta hack YouTube today. Ethically. Of course.

Yeah. Thats all I have. Ill get you guys next time.