- This right here, this Bash script,
is going to make you rich.
Well, not really.
But, using random and some simple math,
it's gonna tell you when you're gonna become a millionaire.
So, let's level up our Bash skills.
Get your coffee ready, time to get rich.
Not really though. We're not getting rich.
Just, just keep watching. (Chuck slurping)
(upbeat music)
Okay, here we go.
The first thing we're gonna do
is flex our already amazing Bash skills
by completing this challenge.
If you can do this, you're awesome.
If you can't, that's okay,
just go back and watch episode one and two.
Number one, in your lab create a Bash script
named getrichquick.sh, and set that sucker up right.
Make it executable.
Number two, create these variables by asking for user input.
And finally, number three,
I want you to echo it back out like this;
"Hello, name. You are age years old."
Pause the video. Give it a try. Go.
Now, if you did this by yourself, that's amazing.
And again, if you didn't, that's cool too.
But anyways, I'm gonna walk through it really quick,
actually really, really fast.
And if you're confused,
just go back and watch episode one and two.
It'll all be cleared up. I hope.
Anyways, let's keep going.
Okay, the first thing I'm gonna do
is go out to linode.com/networkchuck.
Linode is a sponsor of this video,
and they are my favorite place
to quickly spin up a Linux lab in the cloud.
Not only are they fast,
but it's only gonna cost me one penny per hour.
No joke. That's all it's gonna cost.
Seriously, let me show you.
You can follow along, but I'm gonna go very, very fast,
so feel free to pause and rewind.
Actually, let's see how fast I can do this.
Create, linode, images, Ubuntu, region, Dallas,
plan, shared, one gig, name it, password, and create linode.
Whew. (Chuck laughing) That was fast.
How fast did I do that? Time up here somewhere, Nick.
So, within moments this will be provisioned,
ready to go, baked.
But while we're waiting,
let me tell you a bit about why I love Linode.
First, as you probably saw before,
I have like a bajillion VMs in the cloud.
It's how I run my business.
And if you've been watching me for a bit,
it's the place where I run all of my crazy projects.
Now look here. It is done. It's running.
Which means I'm now being charged one penny per hour.
But check this out.
When I'm done having fun, when I'm done with my lab,
all you gotta do is go over here
to the little context buttons and click on delete.
Yeah, I wanna delete it. Goodbye.
A super fast Linux lab in the cloud
and it costs me a penny.
And by the way, if you're new to Linode,
you get $100 credit for 60 days for free.
So if you heard free Linux lab, you're right.
Go ahead and do it. Link below. Linode.com/networkchuck.
Anyways, I need to rebuild my lab real quick.
I'm not gonna make you wait.
Okay. My lab is done baking once more.
I'm gonna copy this command
and launch command prompt in Windows,
or Terminal in Mac and Linux,
and paste that command.
Accept all fingerprints. Password. And we're in.
Step one, make a Bash script and make it executable
and all set up and pretty.
Let's go. Nano getrichquick.sh.
Shebang! Did I scare you?
And then control X, Y, and Enter to save.
And then to make it executable, chmod +x,
and the name of our script.
Let's verify real quick; ls -l.
There's that x. There it is. Got it.
(Chuck slurping) Step one, done.
Time for two.
I'll jump back in my script. Nano getrichquick.
First I'll ask for their name.
Echo "What is your name?"
And then I'll store that data inside a variable named name.
Then I'll say echo, "How old are you?"
And I'll store that inside the variable age.
Task two, done.
And now for task three, the grand finale.
Echo "Hello name, you are age years old."
And that's it.
Control X, Y, Enter to save, and let's run our script.
./getrichquick.sh. What's my name? Chuck.
How old are you? 32.
Hello Chuck, you are 32 years old. We did it.
(Chuck slurping)
Okay, now let's jump back into our script.
Nano. Getrichquick.sh.
Right here, dude, we already laid groundwork
for our script today, so that's awesome.
But here's what we're doing next.
Our task, our goal here, is to figure out
when you and I are gonna become millionaires.
Did I spell that right? I feel like I did.
Let me know if I did.
And I bet you didn't know,
we can actually write a Bash script that's gonna tell us.
Yeah, check it out. Watch this.
Now a couple of things,
becoming a millionaire involves random, dumb luck.
Also hard work.
But for our example, it's gonna be random, dumb luck.
So, new concept time.
Allow me to introduce you to something called random.
Random is a variable just like age and name,
except it's a built in variable.
It's a variable that just comes with Linux.
You don't have to set it.
But what is it? What does it do?
Let me show you. Come on, let's go.
Let's get out of our script, Control + X,
and all I want you to do right now
is type in echo in the command line,
and then $random and hit Enter.
What just happened? Do it again.
Huh? Do it again.
Do it again. And again. And again. Okay, we're done.
What are we doin' here?
You may have noticed that random will randomly,
or pseudo randomly generate a number,
between 0 and 32,767.
And notice how the number
was different every time we called that variable.
Super cool.
And we're gonna use that, that randomness,
to help us calculate when we're gonna become millionaires.
Super rich with golden beards and diamond coffee mugs.
It's gonna be awesome. (Chuck slurping)
But seriously, here's what we're gonna do.
We're gonna take a randomly generated number,
whatever it's gonna be,
and we're gonna add that to your current age.
And that will tell us
when you're gonna become a millionaire.
Foolproof. You can take that sucker to the bank.
Please let it be zero. Come on.
You know what I mean. Anyways.
But before we do that, that's coming up.
And check the time stamp if you wanna jump right to that.
But before we do that,
I wanna talk about what the junk is random.
Well, we already mentioned
that it's a built in variable in Linux.
Pre baked. Ready to eat.
But what I wanna point out
is that it's not the only one out there.
There's a bunch.
In fact, do you remember one that we used
in the first episode?
I know it was a long time ago,
but do you remember what it was?
Let's try it out real quick. Ah, I just hit my elbow.
Let's try it out real quick.
Type in echo, and then $shell. Look at that.
This built in variable will always equal
the current shell we are currently using.
Which is Bash, of course it is.
Which makes you wonder, are there other shells?
Yes, there are.
I made a video about it somewhere up here,
go check it out.
What the shell?
Sorry. That just felt natural. I had to say that.
Let's try some other ones.
Echo $user, your current user account.
Echo $PWD, where you are, print working directory.
Echo $hostname, your local host name,
which is the name of your machine, that's the name of it.
Anyways. And we can actually use these in our Bash scripts.
Let's try it out real quick. In fact, pause the video.
I want you to echo.
Whoa, I just went British or Australian.
My kid's been watching "Bluey" a lot, I'm sorry.
I want you to echo a bunch
of these random variables in your script.
Just do it. Just blast it out. Pause the video.
Unpause. Let's try it out real quick.
By the way, I cannot do an Australian accent.
I've tried so hard. I can't do it.
Nano getrichquick.sh, just down here.
Echo PWD, and we'll do shell, and user, and hostname.
Just all of them in one string.
Save it, run our script. Gotta do my thing again.
Bernard. I'm 94.
And it just threw up all those variables,
those built in variables that we did not set in our script,
they're just set by default in Linux.
Really cool. Really handy.
You will use 'em in the future. You will.
So just remember it.
Now I know you're itching to become a millionaire,
but before we do that,
you have to know some of this stuff about variables, okay?
So just calm down, take a little sip of coffee.
(Chuck slurping)
One really cool thing I wanna show you
is that you can actually create your own system variables.
You can add your own pre baked variables,
like check this out.
So I'm gonna create a variable named.
I just spit water on my.
I'm gonna create a variable named twitter, just like this.
Then I'll have it equal a string, Elon Musk, just like that.
Notice we're setting it the same way we would in our script.
Hit Enter and we're done.
So if we echo $twitter, Elon Musk.
Go ahead and try it for a few of your own variables.
Just do it. Pause the video.
Unpause. Okay.
Just, ah, I keep hittin' my elbow.
Now, this is super cool. We can set our own variables.
But it's not really a big, grownup variable just yet.
He's still just kinda chillin'.
He's not committed to the system here.
You see, because right now we can't use the variable twitter
in our script, it just won't work.
What, you don't believe me? Let's try it real quick.
We'll get back into our script.
Nano getrichquick.
Just below our last echo, I'm going to echo $twitter.
Save it, get outta there, run our script once more.
Iron Man. 65.
Notice there's nothing where Elon Musk should have gone.
He's not there. Why?
Well, because as it stands right now,
this variable that we set cannot be used by child processes.
And yes, our scripts are child processes.
They are your children.
You're a dad. You're a mom. Congratulations.
So yeah, this is kinda stupid, it stinks,
but we can change it.
All we gotta do is make this guy fully committed
and convert him into a full blown environment variable.
Super easy to do by the way, check this out.
It'll be one command, type in export and twitter.
Just like that. It seems almost too simple.
Like, Chuck, are you sure? Is that enough?
Yeah, that's enough. Hit enter and we're done.
Now if I run my script once more, getrichquick,
and go through the whole charade here.
Bam, you see that? My script just used that variable.
My child can now use that variable.
So at this point we created our own variable, system wide,
that any script can use, which is pretty cool.
So now I know what you're probably thinking, coffee break,
let's figure out when we're gonna become millionaires.
Hold on. Not yet.
Coffee break. (Chuck slurping)
There's one more thing you gotta know.
And actually do this with me, just do this with me.
Let's echo twitter, $twitter.
We know what's gonna happen, Elon Musk is here,
we got that.
But now what I want you to do is just type in exit.
We're gonna actually leave our Bash connection.
Connection closed.
And let's hit our up arrow to connect once more,
put our password in,
and now try to echo your variable again.
Echo twitter, just pause the video and go.
Unpause. It didn't work, did it?
If I do it right now, echo twitter, Elon Musk left.
Why Elon?
No, it wasn't Elon's fault. It was our fault.
Yes, we set up an environment variable.
Yes, we made available to all of our child processes.
But we didn't make it permanent.
So as you might have guessed,
I'm gonna show you how to make it permanent real quick.
So that every time you log in, no matter what,
if your system reboots or whatever,
when you log in your variables will still be there.
And all we gotta do is edit a little file called bashrc.
In fact, it's .bashrc.
And by the way, in case you don't know,
in Linux, when a file has a . in front of it, it's hidden.
You can't see it.
Because right now it's in the directory we're in,
it's in our home directory.
And try to look, look at it, type in ls.
Well, I don't see anything but our script,
but if we put on our secret goggles and do ls -al,
something's gonna happen.
Whoa, look at all these hidden things,
including Mr. Bashrc right here.
I told you he was there. I'm not crazy.
(Chuck slurping)
Now, what is bashrc?
Well, it's essentially a script
that will run every time you log in.
It will set things up for you, make things pretty,
make sure everything works for you.
And this is where we can set our environment variable
and make sure it's set up for us every time we log in,
all we gotta do is edit that file.
Simple enough. Let's do it real quick.
How do you edit a file? Well, you use an editor; nano.
And we'll do nano .bashrc.
Man, we're doin' it.
And here's all we have to do; type in export,
which we used before to export an environment variable,
and we'll say export twitter equals the string Elon Musk.
I spelt it wrong, Elon Musk.
There we go. That's it.
Hit Control X Y Enter. Edited, done.
Now if we try it right now, it's not gonna work.
Trust me, it's not. Try it. Didn't work.
Why do you think it didn't work?
If you answered, well, the script runs every time we log in,
so we haven't tried to log in again.
You're exactly right. So let's exit, let's exit outta there.
Then we'll jump back in. And now if we try it.
Here we go, fingers crossed echo $twitter.
There it is. There's Elon Musk.
I know he wouldn't leave us.
Okay. Coffee break.
We did a lot. (Chuck slurping)
We made our variables as permanent
as we could for ourselves.
We can use this sucker everywhere. Elon Musk is everywhere.
But enough about variables, let's get rich right now,
or at least find out when we're gonna get rich.
Let's edit our script.
And actually, before we edit our script,
I wanna cover one thing.
Again, I mentioned before that we're gonna get rich
by using the random variable.
And in the beginning I also said
we're gonna use simple math.
Because you can do math with programming, with scripting,
and especially with Bash.
And as I mentioned earlier,
that simple math is just gonna be that random number
plus our variable age.
Now we do have one problem
that you probably realized, right?
Random by default will generate a number
between 0 and 32,767.
I can't believe I memorized that. I'm awesome.
I'm like grain man.
So let's say it does generate that last number plus our age.
Well, we're dead. That's not fun.
So we need to change which numbers
it will randomly generate.
Here's how we do it.
This will also be a math problem,
and we're not gonna do it in the script yet,
we're gonna do it right here in our terminal.
So type in echo, 'cause we're gonna echo this result,
and we're now gonna start what's called
an arithmetic expression,
which is just a really fancy word to say,
we're gonna do some math here, guys.
That's it.
So type in this; dollar sign and then do an open parenthesis
and one more open parenthesis,
and then do a closed parenthesis
and another closed parenthesis.
And then we're gonna put our stuff right there
in the stinkin' middle.
And with this, we're just tellin' Bash,
hey, get ready for some math, buddy.
We're gonna add some stuff.
We're gonna subtract some things.
Divide, multiply.
It's all gonna happen right here in the middle.
That's all we're doin',
we're just tellin' Bash we're gonna do some math.
And inside this is where we're gonna do our problems.
For example,
I'm gonna do space 2 + 3, let's try it out.
Bam. We just did Bash math.
Which is way better than regular math.
Don't tell your teachers if you're still in school.
You don't need a calculator,
all you need is a Linux terminal, man.
Now, again, keeping in mind that we're saying,
hey, this is an arithmetic expression
by doing a dollar sign
with our opening and closing parentheses double.
And then we have our expression, our problem in the middle.
Now I added space there to make it nice and clean.
You don't have to have space. Just makes it more readable.
But go ahead and try a random problem.
For example, you could do 2 X 3, with our asterisk.
Multlplication. Easy for me to say. Multiplication.
And then back slash for division.
No, wait, not back slash, forward slash, my bad.
There we go.
Now notice how when we do 2 divided by 3 it gives us 0.
Bash by default will not do floating point numbers,
which is just decimal, like .5, .3, 3.2.
And if we use our calculator,
2 divided by 3 is .6666666 all the way to 7.
So why didn't it tell us that?
Because it doesn't do float numbers by default.
And the same thing goes for other division problems,
like let's say 10 divided by 3.
3 does not evenly go into 10,
so we'll obviously have a remainder,
we'll have a fraction, a decimal.
But we get a whole number; 3.
Which brings us to a tool we're gonna use for our random,
to be able to calculate that random number
in a range we want.
There's a little thing in programming, and in math,
it's a math thing, called modulo.
It's the percent sign.
So instead of division, multiplication and plus sign,
we're gonna use modulo.
And what this will do is tell us the remainder
of that division problem.
So if we run that real quick,
10 modulo 3, it'll give us 1.
Which, 3 going into 10; 3, 6, 9.
We've got one leftover to make that 10,
that's the remainder.
So now we're gonna do a math problem with random.
So check this out.
I know it's a long, drawn out way to get to this.
Trust me, it was necessary.
So we're gonna echo,
we're gonna start our arithmetic expression,
I can't even say that,
with dollar sign open parentheses, parenthesi,
and we're gonna call our variable random.
And then we're going to modulo, percent sign,
and let's just put the number 10.
That's what we're gonna do.
And then closing parenthesis. Let's try it out.
Huh. Three? Let's do it again.
Five. Nine. Zero. Eight. Eight.
We can keep going, just forever.
Now you probably guess what's happening.
Doing that gives us a number range of not 0 to 32,767,
it gives us 0 through 9.
It's saying 10 digits.
And you know computers start counting at zero,
so 0 through 9, that's 10 digits.
That's what we're doing with this modulo 10.
Now, if you wanna know, hey, how does that work with modulo?
That's a different video. That's not this one.
Just know that's what we have to do.
That's the pattern to make this work.
So go with that same pattern.
If you wanted a random number between 0 and 19,
so we put like 20.
That's what we get. Come on, gimme somethin' high.
There we go, 14. 16. 18.
That's the pattern. So now here's your task.
You got the skills. You got the tools to do this.
You already know.
What I want you to do is take what we've learned here,
and I want you to add a random number
between 0 and 14 to your age.
In fact, I want you to put that inside a variable,
so have that variable equals something like getrich,
and then echo out a sentence that says,
hey, something like this.
I don't know, I have thought about it.
Hey, you're gonna become a millionaire at this age.
Do it right now. Pause the video.
Unpause. If you did it, by the way, that's so cool.
I'm gonna walk you through it real quick right now.
Coffee break. (Chuck slurping)
So, let's get back to our script.
Nano getrichquick.sh. Done here.
I am gonna remove all the stuff about twitter,
and all of the extra variables.
We're done with that.
I'm gonna add a little space here, little time.
Sleep two. And then here comes the calculation.
The variable will be getrich.
I'm gonna have that equal
dollar sign open parenthesis open parenthesis,
get a little bit of space, we don't need it, but I want it.
$random modulo. What did I say? 0 to 14?
So I'm gonna do modulo 15.
Givin' me the numbers 0 through 14.
Now, here's where it may have gotten you a little bit,
not a big deal.
For this equation, and it's really just plays into math,
we need to enclose the random modulo 15
in its own little parentheses.
You know, math.
So we're gonna close those up to isolate them.
So now we're taking random modulo 15, that result,
and adding it to our age.
So we'll do a plus sign, our variable $age,
and then we'll add two more parentheses at the end
to close it out and make sure they match on both sides.
That should set our variable.
Now all we have to do is echo this out.
Echo, I'm gonna do name.
Name, you will become a millionaire
when you are getrich years old.
That's it. That's the thing.
That's how we're gonna know when we're gonna become rich.
Aren't you excited?
So, real quick, before we enter,
I wanna add some build up, just some fun here.
I'm gonna do echo calculating,
and I wanna do a loading screen
to make it build up the suspense.
That's more fun. Okay, there we go.
A nice little fun loading screen for us
to build up a suspense.
Okay, here we go. We are ready to run our script.
./getrichquick.
Okay. My name is Chuck.
I'm 32. I need some coffee.
I'm 32. Here we go.
Oh, it's calculating.
When I'm 39. Okay, I can deal with that.
I can deal with that. That's fine.
Now I did spell millionaire wrong.
I realize that now. That's my bad.
39. I can deal with that.
Post below when you're gonna become a millionaire.
How old will you be? Let me know.
Whew. Okay.
Now in this video we covered a lot, almost too much,
but my idea was there and I had to do it,
so I apologize for the length.
If you liked it, let me know.
But here's what we covered.
We covered variables, more importantly, system variables.
Variables that are built in that come baked into Linux,
which are awesome, like random, and PWD, and user.
But then we also saw how we could make
our own system variables,
but we had to do a few things,
especially if we wanted our children, our scripts,
to be able to use them.
We had to convert them to environment variables,
export those bad boys.
And then if you wanted them permanent,
every time we logged in, we had to edit the bashrc file.
And then more importantly,
we found out when we're gonna become millionaires
by using random and some simple math.
We saw that math can be accomplished
with a arithmetic expression,
just a dollar sign, parenthesis parenthesis,
equation, parenthesis parenthesis.
Either way, super fun, super powerful.
Now there was one more thing I wanted to put in this video,
but I just couldn't.
It would make this so stinkin' long.
But I did a lotto thing,
where I played the Texas lottery
and we got our lotto numbers and we see if we won.
It was a fun little way to practice this.
If you want a extra lab, a extra way
to solidify these skills, check it out.
Link below to Network Chuck Academy. It will be there.
Now also, the skills we learned here, specifically random,
we're gonna be using in the next video,
and the next video's a little crazy.
It took me way too long
to just kinda play with it and have fun,
but we're gonna be building "Elden Ring" inside Bash.
That gonna be stupid, but it's gonna be fun,
so I'll see you there on the next episode.
And if you're watching here on YouTube and you're like,
man, I hate waiting for the next episode.
Well, guess what?
The next episode is already on Network Chuck Academy,
so go check it out, link below.
And also, have you hacked the YouTube algorithm today?
Let's make sure you do.
Hit that like button, notification bell, comment, subscribe.
You gotta hack YouTube today. Ethically, of course.
Yeah. That's all I got.
I will catch you guys next time.
(upbeat music)