Welcome to section five.
In this section you will learn about scripting basics using Bash.
This section is not meant to be a definitive guide to bash however,
but rather appointed approach only providing what you'll need.
You'll start off by learning bash fundamentals like data structures,
flow control and variables.
Then you will take that knowledge and create some administrative scripts.
In this video you'll learn about Bash and how to make a bash script.
Our plan is to explore the fundamentals of the Bash shell.
We'll learn about common paradigms like arrays,
flow control and functions by reviewing the provided code samples. Finally,
you will create a game and bash with some of the knowledge you have just
learned. Okay, let's get started. Okay,
here we are back in the terminal again.
And so you've been using Bash this whole time and maybe you recognize
that maybe you didn't.
So Bash is just a shell that you use when you log in and to be able to
interact with your os. It's also a command line interpreter.
You could run commands in the Bash like Ls or CD like you already have,
or you could put them into a script and run that, which is what we're gonna do.
So I've included all the scripts in the GitHub repo for this section under
section five. You can grab them by installing Git on your e C two instance.
So if you're unfamiliar with Git,
it is a distributed version source control system.
And what we're gonna do is we are gonna clone uh, the repo,
which you obviously already cloned it onto your local host.
We're gonna clone it here on our EC two instance. So to do that,
let's first start out by installing Git and we'll have to use pseudo again cuz
we need elevated privileges and we're gonna say Yum, install Git.
And then after that just hit enter
and gonna ask us are we sure it's gonna install these dependencies? We are.
Okay, we're installed,
we clear the screen and so now we have the Git command line. As you can see,
I just tested it and we didn't give anything so it just gave us the help.
All right,
I'm gonna go ahead and paste this in so you can go ahead and clone it.
I'm sure you have it for when you did the beginning of this course.
So I'm gonna pause the video here, let it clone cuz uh,
it may take a little bit when I come back we'll have it already on the system.
Okay, so I cloned it. As you can see here, I ran LS and we are all set.
It is on our e C two instance.
And the biggest reason for this is this is just gonna be easier for us to review
the source code examples here because this course is supposed to get you up and
running as fast as possible.
It's not advantageous for us to go and write every single one of these scripts
by hand to learn bash.
That would be great for a course that was explicitly made just for Bash.
But since we're just trying to get uh, some foundation stuff,
I've took the liberty of adding those examples like our, of our,
like I said earlier,
and we're just gonna review them and I'm gonna go over some highlights of what I
think is important about Bash that you should take away.
At the end of this video we are gonna write a a script together. Um,
and we'll do that from scratch. Okay, so let's get started.
So one of the things you're gonna need for this video as well is you're gonna
need an editor. I'm gonna use the editor right here on my e c two instance.
I'm gonna use Vim, which v i m if you're unfamiliar. And um,
there's also nano available N A N O. But I'll leave it to you which you prefer.
You can't go wrong with either one.
If you're unsure how to use either one of those editors,
please pause the video now and uh, go ahead and do some research before uh,
proceeding. Okay,
so the first thing that we're gonna do here is we are going to open
the dummy sh file under this section five directory.
And so this is a file we're gonna go over some quick basics of bash
that don't kind of fit into the other uh, data structures and flow control.
But this is also, I'll give you an exam, uh, uh,
a chance to go ahead and like test your editor out as well.
So let me open mind and vim.
So I type vim and then the file wanna open that hit return. Okay,
so line one here and this is the first thing that should jump out it to you.
The first line is what we call a shebang.
And so the shebang in any type of of scripting basically tells
the os how it wants to interpret the rest of this file.
And in our case we want the rest of this file to be interpreted by Bash. Um,
obviously we're using bash commands so that makes sense.
So you can also notice that this line starts out with a pound sign
and you can see those, these are in bash are comments.
This is how you make a comment. You can see here on line three I made a comment.
So those will be ignored by the bash interpreter. You get down to line five.
Here we have what's Echo and then we just say Hello everyone.
And the echo command is basically how to print something to the screen.
We go down here to line seven and is an exit command.
So in Bash you will want to use a lot of exit commands, um,
because if your script needs is not doing something there was an error needs to
get out of the script early,
you can use the exit command and this way you will also allow to alert the
caller that there was a problem in Bash and in in Unix and Lennox General.
Usually something when it exit zero is means everything was great. We don't,
we don't think there's any problems. It is a clean exit.
Anything in non-zero exit would be considered something was went wrong. Um,
and also, you know, you don't have to use exit one,
you could use an exit five but then you know,
you would have to then have a way to map what those exit codes mean to
some sort of missing file or some other type of error base. But okay,
so we have an exit one here on line seven and if we go to line nine here we have
another echo.
And then this echo you can see it says this will not be printed and the reason
why it won't is because line seven is going to exit out the,
so line nine will never run.
So let's go ahead and we're gonna get out of this file.
I just wanted to show you comments how to print to the screen and how to get out
of a script early or to um,
announce that it was a good exit with an exit zero.
So in them I do a shift colon, um, shift colon queue,
which will go ahead and quit and get us outta here. Um,
and so let's go ahead and run this. Um, if you've clone this repository,
all the files are already executable so we don't need to do anything.
But if for some reason it doesn't execute, use the chima cam,
use the chaud command and you could do this if you get a
permission deny trying to run it,
you could do a chaud plus X which will make it executable.
I could do it and it's item potent, it won't hurt it. So, um,
but we'll get rid of this and we're gonna run this command right from this
directory in section five. And as you see we only got hello everyone.
We didn't get the last line that says this line will not be printed and in bash
there is a built in variable that
allows us to see what was the last exit code of the last command we
just ran.
And in this case we ran the dummy script and as you could see when I echo
whatever was in that variable right there, that special,
that special variable with the doll sign. Question mark,
you can see we have the one which is exactly what we saw in that following line
seven was exit one. Okay,
so I'm gonna go ahead and clear the screen one more time.
I'm gonna move on to variables.
Variables are aliases that can hold information about something else. Um,
I think this will do a lot better once we open it up and look at some examples.
So let's go ahead. I'm gonna do vim
section five and then we're gonna look for the variables file.
So Vim, hands-on Lennox for DevOps,
cloud engineers directory section five sub directory and the variables SH five.
Okay, let's open this up. Okay,
so line number one, starting to look familiar.
Hopefully the shebang, we need it every time.
Line two just has an empty comment.
So lines three through five are variables and you could tell they're variables
because you have uh, whatever it is in this case. Line three is CPUs.
You have an equal signs and a one and you notice there's no spaces or anything
between them. And here we say CPUs equals one line four,
we say memory equals 1,024. And then use a name.
You could see that we use double quotes to denote that this is a string that we
want to represent and it's better to be explicit than implicit. Here.
It's okay to uh, call out your types that way. And as you can tell here,
the variables are lowercase. You may run,
you may run into variables and other systems that are uppercase,
but Bash itself uses environment variables that are all uppercase
and to avoid accidentally running into, uh,
confusing yourself and other people of which is the system variable and which is
yours. If it's a local script, these variables are local,
go ahead and you should keep them lowercase. Just be consistent.
That's all that really matters here. Okay,
so we have these three variables and we go down the line seven and eight and
then this is us just printing them out.
The one thing that's worth pointing out here is I want you to show here is this
dollar sign, curly braces inside the double quote string.
And what is special about this in bash is whatever is inside these double curly
braces, it means to expand.
And so it basically takes whatever the variable is in this case CPUs,
which is one, it'll just expand this variable and replace this whole
placeholder here with the, with the number one inside this string.
Exactly the same thing for memory and username. Alright,
so let's go ahead and close this.
Let's run it and we obviously have a good idea of what it should print up.
Let's go ahead and do it anyway. So shift colon Q.
So we'll quit out of this file.
I'm gonna hit the up arrow and then I'm gonna hit control A to go to the front
of the line and then I'm just gonna delete the vim command and then I'll run
this file.
And you can see this host has one CPU and 1,024
kilobytes of memory. The active user is EC two users.
So you could see how we can grab variables, set them,
and then display them out with the echo command to the rest of the system
or to the screen I should say. Okay,
so let's move on. I'm gonna clear the screen and then we're gonna move on to it.
Uh, one of the, the data types,
the most main data type you're gonna use in Bash and it's arrays.
So there are two types of arrays you can use in Bash.
The first one is a dimensional indexed array and the other is an associative
array.
So a regular indexed array is like probably one you're used to seeing in other
program language. Um,
you can have a list of something but you can access it by using an integer based
index, you know, 0 1, 2 3 and it'll give you the element at that space.
In the array associative arrays,
you string indexes like keys so you can reference the elements of the arrays,
like almost like a hash or a map except it's still an array and you use
the string as the key instead of a positive be integer. Okay,
so let's go ahead and we're gonna open up the arrays file and that
is in section five arrays.sh.
Okay, so line one, our shebang. Um, I'm,
I'm not gonna keep saying that over and over again,
but I just want to drill in that the shebang line is extremely important.
Line three, you can see we have a declare keyword.
So the declare keyword is a bash keyword which allows us to declare array.
And as you can see here, we're using a
minus small A, right?
So that's for the index based the integer index based array.
And down here on line 16 use the capital dash A for the associative
arrays. Okay,
so up here we're gonna call this array distros and this is just Amazon Lennox,
I mean sorry, this is just Lennox Distros. And so lines four through eight,
I am just setting it. For example,
at index zero we're using Amazon Lennox at index
one we have a Buntu index, two red hat,
so on and so on up to index five where it's arch.
So line 10 is where we're actually printing it out,
but we're using this special built in character,
the at symbol that deals with bash arrays.
And what this tells bash to do is is it explode the array on one line.
So it basically says go ahead and put all the elements of
of this array on one line eliminated by space.
So as you can see here we have the dollar sign curly braces
again,
meaning whatever's inside the curly braces explode out and then
it'll dump it in the strings. Now line 11 is interesting.
So let's say that you did your raise but you realize that oh
Devin should not have been included in this list for for whatever reason you can
use the unset command and you pass it the index and the array that you want and
it'll take the Debian version out. So we could see on line 10 here,
we're printing out all of them, which should be Amazon Lennox through arch.
We take Debbie and out and then we print it again and we should get the array
minus Debian.
And this right here is just a simple line separator that I've put in there just
so you can see the difference between the both print statements of the array,
the two echo statements, sorry. Okay, so let's go down to line 16.
This is where we do the associative array and we're gonna call an associative
array just command and line 17 and 18 is where we're
actually setting the array. Uh, the command's array.
And you could see instead of using zero,
we're actually using the command name in this case would be Ls.
And then the value which will be in the array is where it is located
on in the directory.
So you could say down here on line 18 we're saying the cat command is located in
the bin directory. And so all those,
this is a way if you are not using the positive integers and sometimes you know
that might be a little bit harder to understand or order or uh,
quicker lookup means something to you or you need to be able to call it by a
name. This is a good, you can use an associative array. So down here on line 20,
we're just showing how you can reach into that array
in between the dollas sign curly braces to expand it and just use CAT as the
index. All right,
so let's run this and let's see everything that we just learned about arrays out
on the screen. So shift colon q quit this file.
I will clear the screen,
put the up arrow to get to our command control A to go to the beginning of the
line, get rid of the vem command and hit return.
And so you can see here this was the first echo command where we exploded
the whole array out and we printed out each of the elements and there's five uh,
six there.
There was the line separator and this was after we unset debian.
So if you look down here, there's only five in this one,
Debian is gone from this line. Okay?
And here you could see where we'd use the associative array where we said the
CAT command is located in the bin director and we use CAT as the index. Alright,
that is it for arrays for now. So let me go ahead and clear the screen.
And so the next thing that we're gonna get into is branching.
So we need some sort of logic base when when we're talking and moving around the
code,
we need to be able to check values of things and do something if a certain
criteria is met. And so we'll start out with if so, the if statement.
So the if command is a branching command that you can use to make logic
decisions. So like if this, then do that.
Or you can have multiple branch strategies, you have multiple if statements,
nest if statements like if this thing do this,
otherwise do this other thing.
So let's go ahead and let's look at the if file and it is
under section five again and it is called if sh I'm
opening it with the vem command once again. Okay,
so as you can see on line three we create a variable called CPU and we
assign it a value of six.
And if we go down here to line six,
the bash if statement uses the keyword if,
and you can notice these single brackets right here.
So these single brackets are also built into bash and what they mean is they
mean test.
So they're basically saying test whatever this expression is inside
these brackets, test that and if it's true then do this first thing,
then do line eight. If you notice that if it's false,
there is nothing else you can do on that line that it just won't print it out.
And in this case it's just printing out it's suitable virtualization.
So if the variable CPU and you notice that we have to reference
the CPU variable with a dollar sign.
So Bash knows that we mean the variable CPU and not just a string.
We use a dash gt which is greater than there's obviously dash,
less than dash greater than or equal to dash,
less than equal to or just uh, equal or not equal.
And those are used,
these dash ones and why you want to use those instead of the other operators
that you think of like a,
a right hand greater than arrow and other things is that Bash uses these for um,
numeric comparisons. And so it's good to just stick with the uh,
the paradigm that Bash expects. The other way is fine too.
This is probably more portable on bash I believe so it's probably worth using
it. But anyway,
we could see here we're saying if C P U count or whatever C P U is,
if it's greater than four, then print this line out. So line 11,
just an empty echo, I'm just making some space here.
So let's go on to something a little bit more interesting here.
So on line 13 through 18, which I have highlighted right here,
I'm saying if the CPU is greater than four, which is the same line of six,
then echos suitable virtualization.
But here we have the else command on line 16 and we're saying if it's not
suitable, so if this test right here is CPU greater than four,
if it evaluates false, then we run line 17.
And of course we just echo out a silly uh phrase here.
So inside these bracket tests and we'll jump down
the line 21,
this dash F is a built in test inside these brackets for
dash. And what this allows us to do, it says does this file exist?
There's a dash D for does a directory exist? There's a dash z um,
to see if an argument is populated. There's a whole bunch of 'em,
I won't go with 'em all over here.
You can find on using the demand page or you can also Google them of course,
but these are super handy if you wanna test for edge cases or corner cases of
something being there or not there instead of having to write your own.
So basically what this if block does from lines 21 through 26,
it says if the Aira file exists then echo file exists,
otherwise echo the file does not exist. Okay,
let's go ahead and close this file,
which is shift colon Q and we're gonna run it. So I'm gonna clear the screen,
I'll hit the up arrow once again, control A to the front of the line,
run it and you could see the first if block was suitable for ation because six
was greater than four, you could see the second one,
even though we had the Ls, was still suitable for virtualization.
But if for some reason we changed that CPU variable to say three,
which is less than four, then we would've got this line would've never printed.
But this line would've said your mileage may vary.
And here we tested with the dash F builtin to see if the file existed.
Okay, so that is it for IFS and if else's, let me clear the screen here.
So the next thing that we're gonna go over is loops.
So loops are great when you need to do something over and over again. Um,
and you need to uh,
do something to maybe a variable or a file
or go fetch a bunch of things and something that's repetitive.
So there's two loops you can use in bash.
One is the four loop with the four key word f o r and the other one is the wild
loop and we're gonna see both of them in its file.
So let's go ahead and use Vim again.
We'll go section five and we're gonna say loops.
Okay, so there is a lot going on here. Um,
so what we'll try to do is just go through the parts that that are new to us
instead of the old. So if we scroll down, um,
you can use in vim,
you can use the arrow keys down or you can use the J command and the J command
will allow you to go one line down at a time. Okay,
so let's go down to line 12 and you can see so uh,
lines three through eight we're just creating the distros again an array that
which you saw the rays have followed. Nothing different there.
And the reason why we're just using that so we can use the four loops and the
wilds to print over stuff right here. So lines 12 to 15,
we have four loop,
which is with using the four key word and distro rate here is just a
temporary variable placeholder. And you could see here that we're,
we're doing the dollar sign curly braces of the distro with the at symbol
meaning expand all the members of our array onto one line and then
let's loop over 'em. And so for each pass of this loop,
distro will become one of these and there's a weird,
um, not a weird, but there is a small inconsistency here. When we run this,
you'll see it. Let me know, uh, pause the video,
take a look at the code and tell me if you can spot it. And then we run it.
I'll explain it. So basically for each of the loops,
each iteration of the loop, we do in this case just line 14,
we just echo whatever distro variables holding at the time and then we use the
done to go ahead and be done with that block. Okay, so that's the four loop
line 17, just the line separated.
So you can see the differences between each four loops.
So line 19 through 27,
or sorry 20 through 27 is another four loop over the Lennox distros.
But this time we're using a keyword called break.
And so basically what we're saying on lines 22 is we're saying if the distro and
you notice that we're using the dollar sign and we're putting the variable in
quotes here because we're comparing strings.
So both sides need to be strings and bash when comparing them.
We're saying if distro equals red hat then break.
And what break does is going to stop this loop completely.
You're gonna jump right out of the for loop.
And as you can see what will happen is you'll get,
you'll print out the first indices, the second INDs,
you'll get to the third industry indices and you'll see line 22 will say,
Hey is if distro equals red hat, then break.
So you're only gonna end up seeing this printed out and we'll see that when we
run it. All right,
so let's jump down to line 29.
And in them if you want to go to line numbers,
you can do a shift colon and then the line number like it did here,
which I just did shift colon 29.
So this is a wild loop and wild loops keep doing iterations in a loop until
a condition is met. And in this case it's inside these brackets.
Remember this is a test.
So test says variable count is less than 10
and you can see right now count on line 31 variable is set to zero.
So this loop is going to actually start because it is less than 10.
And then here what we do on each iteration loop is we echo count
whatever count is then line 35,
this is a special operator and you can see it's the double parenthesis.
And then inside the double parenthesis what that tells bash is that this is a
mathematical expression. And here we can do count plus plus.
And so a count plus plus means it just means increment count by one.
So once this will keep going iteration,
the first loop iteration count will be one second iteration count will be two,
third iteration count will be three, so on and so on until count hits 10, right?
Then count is not less than 10.
So then this wild loop stops all on its own and then it proceeds to the next
line in this case would be line 38, which is echo countdown.
All right, so let's go down to line 40 here. So you can see,
so this is the last while loop. And so this one is just a little bit different.
Instead of counting down,
we're gonna count backwards like you would on a launch.
And so here you can see count to set the 10.
Now the variable and then we say when count is greater than zero,
it'll actually circumvent this, this loop.
And right now it's 10 so it would just keep going.
And we do the same thing we did in the other loop. We echo the count,
we do a mathematical operation,
but this time we do a minus minus which means to decrement to take one away from
count. Each iteration here on line 45,
I just put a little small sleep of 200 milliseconds and that is just to give the
effect of a little bit of pause as it counts down to give it like that,
that timer aspect. And then you,
while once that timer reaches zero,
then this whole wild loop will end,
it'll jump back out of the wild loop and then you'll end up with four, uh,
line 48 which is just printing out to blasto. Okay, that was a lot.
So let's go ahead and run it and let's see it happen. So shift colon Q.
All right,
we're gonna hit the up arrow control A and then let's go ahead and run the loop.
Okay,
so you saw we printed out the array first Amazon
Lennox TU arch here we printed out Amazon,
Lennox a Buntu and then we have this count up we counted from zero to nine
and then we did a countdown where we counted from 10 to one and you could saw
that it paused between and then we blasted. Okay,
so that is it for loops. Let's move on to the next paradigm.
And this is going to be switches.
So sometimes the if else logic like you saw can get complicated,
it can be error prone and sometimes hard to read.
So BAS gives us another flow pattern called switches.
So let's go ahead and open up the file for switches. It's in section five
switch.sh. Okay,
so here we're doing something a little bit different.
So line three here we print out,
we say enter a person's name and now we're using this new bash function
or bash command keyword called read.
And what this does is it takes something from standard in and in this case it'll
prompt, it'll block waiting for you to type something into the terminal. Um,
and in this case it's asking for a name, we're asking for a person's name.
And then what happens here on line four,
whatever it reads in from the terminal with a return statement,
once a return is hit,
it'll go ahead and apply whatever we typed in into this user variable. Uh,
if you come outta the line six here we use a switch statement and here it says
case.
And so what case statement basically does we case on whatever the
value of user ends up being And then the rest of this
syntax, while it looks maybe a little bit interesting,
you could see here that we use the EAC to close the case,
which is case backwards. And if you notice if has fi,
it's the same thing and this alerts bashed to know that we're done with this
block. But so in the case statement, whatever this value is,
if it matches a Kira, then we echo this line,
we echo line eight and these just mean to go to the next one.
These are just separated as ca switch has a specific format.
So you gotta follow it uh, to the letter here. If it doesn't match a Kira,
it'll then hit this one, it goes top down. Okay?
If the user is Leilani, we just, we echo uh,
sentiment here and then the star one right here is basically a default and
catchall.
And this is a very good habit to get in because if it didn't match any of these,
it would just end out out of this and nothing would happen.
You would jump outta line 16 and there was nothing else in the script.
So the script would just end and nothing would ever be printed on screen except
the lines three and four or lines three. So what this basically says,
well if you don't match any of these things that I have in here,
obviously you can have as many as you want.
I only put two in here just for example. But basically then you print out like,
I have no idea. All right, so that is our switch statement. That's good.
It cleans up your if logic and it makes it a little bit easier to read and you
can do matching from top down. All right, so shift colon Q,
we'll put this file up arrow control A,
let's run the switch command. So as you see here,
it asks us the enter the person's name and the reed command is blocking now,
which is waiting for us to type something. So I will type the name Akira,
hit return and it says I do not know them that way, right?
So we run it again and let's say I say Bob, we we get the,
I have no idea who Bob is and that is the catchall that we were talking about,
the asterisk one. Alright, lemme clear the screen.
So the last file we're gonna go over is called function.
So functions allow you to encapsulate behavior into an isolated call.
It makes your code easier to read and you can put light things together so it's
easier for people who come up behind you look a descript a reason about it.
So let's go ahead
and open up our section five and with safe
function, okay,
so functions like on line five and live line nine and
line 17 to begin with a name, open print,
close print. And here we're using underscores here it's more of a convention.
You can use whatever you want, but this is what I,
and whatever's in the function is encapsulated here by curly braces.
So the function on line five basically is going to echo the
host name and you can see how this variable with the doll sign all caps.
That is because that is a bash host provided environment variable,
not one that we created.
And it'll just give us the host name of our EC two ones here on line
nine. We create a home get home function.
And you notice how these functions have names that describe what they do with
that's, it's just a really good um,
way to think about how you wanna write functions instead of making it very
simplistic,
it it'd be hard a year from now if you go back and look at this may be hard if
it's named FU or uh, you know,
Johnny wanted this and at least gives you that. Here we also echo,
if you notice this doll sign all capital's home because that is another
environment variable that is set by the host, by us, uh, for us that we can use.
And then line 13 is just echoing it out.
But here you notice when we're calling a function,
what's most important here is we're using a dolls sign parenthesis in here to
execute the function because instead of expanding a variable or putting it out,
the parentheses basically execute the function for us and then whatever the
results are,
it puts in place of it and it's the same form the get home directory.
Okay? So functions could take parameters just like commands.
And in here we do a list files function and then we echo
this Dollas sign pound basically gives you the number of arguments that are
passed into this. So if you pass in five arguments,
this line 18 is gonna say there are five arguments.
How do you get an argument in in your function?
You use the dollas sign one if you want the second argument you would do doll
sign two if the third argument dolls sign three, and so on and so on.
And so here we've recreated the LS command, we've basically wrapped it,
I shouldn't say we recreated,
we wrapped the LS command and we're saying go ahead and whatever directory we
pass into list files function, go
ahead and use the Ls and the al flag.
And here on line 23, we're calling the function,
but we're passing this get home function to it.
So we're using a function to pass a,
to give us a parameter to pass to one function.
You could have just as easily made a string here called slash home slash
EC two user. But in this case I wanted to show how you can use them, uh,
both like this. Okay? So let's get outta here and let's run it.
We'll see it in action. So shift colon Q
up arrow control A and let's run it.
So you can see this first line up here is hello
I am ip which is, this is the host name. This is our e C two host name.
And then here you can see I live in the home ec two user home directed and that
is that built in home variable that we're using
here you can see there are one arguments we only passed in the get whatever the
get home function return, which was just one parameter.
And the argument one that we passed in was this.
And then remember we called LS minus al on dollar sign one and then here we go,
we, we got the printout of the C two user home director.
Okay, let me clear the screen. Okay,
so now it is time to put everything we've learned so far about this, you know,
quick crash course and bash in some of these fundamentals and we're gonna make a
simple guessing game. You know the ones that say like,
guess the number one through 10, one through a hundred, whatever.
And then if it you pick a number, it says too high,
too low until you get it and then you get it, you win. Okay,
so I provided the full source code for this uh, game. It's in the section five,
but I thought we'd try to do this one together from scratch.
And so we're gonna open up and then I'm gonna just call it GG for
guessing game. And we're gonna,
I'm gonna do this one not in the clone repository but outside in my home
directory and we'll go ahead and do that. And the first command,
as you guys know, bin Bash.
And so the first thing we're gonna do is we need a number, right?
So we need computer to generate a number for us and
we're gonna do a random number obviously.
And so the double parenthesis,
remember cause we need to do a mathematical operation
and I can't spell random, but we'll try that again so random.
And then we're gonna use this modus command,
which basically means we're gonna divide it by 10 because what we want it to do
is we don't want the number we're gonna do,
we're gonna pick a number one through 10 to guess and we don't want the number
to be ever be larger than 10 and we can't let it be zero or ne, you know,
whatever negative.
So we're gonna say plus one here to make sure that we are
within range and we'll close the double parenthesis.
So random by the way, you can see the dollas sign random because that is a,
I'm sorry, that is a built-in environment variable as well.
And it just gives us a random number. We'll do modules 10,
which means divide it and by 10 should always give us a number between one and
10 with the plus one. All right,
so now we're gonna tell the user,
the game player what we're gonna say and we're gonna say guess the number
between one and 10 guests, a number from one to 10.
And then we're gonna say, guess we're gonna have,
we're gonna keep track of the guests.
And so right now we're just gonna say the guess is zero.
This is what the user will guess in.
Now we're gonna use a wild loop here because we want to,
we want the script to keep asking us over again.
We want it to say too high or too low. And that'll work with the wild loop.
When the condition's met, that means we won the game.
So we'll do while we'll do the brackets cuz we're gonna test,
we'll say while our guests does not equal.
So the dash n e does not equal the number,
which is what the random number is, right? While that
we want to do this,
we wanna read the guess in and remember Reid is gonna read from
whatever the person types in. So they'll be typing in the guess here.
And then here what we want to do is we wanna say if,
if guess
is less than the number that was random,
then we want to echo it's too low,
right? And then fi,
we also wanna say if we'll do another if statement here and we're gonna say,
if guess
is greater than the number that was randomly chosen
for us,
then we're gonna echo it is too high
close, oops, close our if block
and then we're done with our wild loop.
So that means if we have finished this wild loop,
that means that the, the guest number is equal to the number,
the guest is equal to the number, which means that you have won,
you have won the game.
And here we can just do an exit cleanly just for safety.
And then here we're gonna use a new command,
which is gonna be the shift colon X.
And what this will do was write and quit at the same time. Okay?
If I tried to just run this like this,
sh you can see permission denied.
And this is because if we look at GS H, you could see, oops, sorry,
let me do the dash L for the Longview. You could see that it's not executable,
it's read and writeable, but it's not executable.
We don't have an X in our permission.
So one way you could do this is you could do a chima plus x
the file you want to do in this case the GG h.
And then let me clear the screen here and let's see if it works.
So we'll do dot slash gg sh,
which means we want to run it exactly where the file is.
And because it's now executable and we have that shebang on line one,
it should just work. Okay, so it says guess the number from one to 10.
I'm gonna say five. So the number's too high. Say three,
two A was two, we won the game. Okay?
Now I know that's more of a contrived example,
but you put all the paradigms together, you put a wild loop, variables,
mathematical expressions, exits, echoes.
That should be a good foundation to get you going riding.
And obviously the rest of this videos going forward,
you're gonna do a little bit more of the administrative type of task.
Now that you have a bash fundamental, okay, let's go back and do a quick recap.
This was a long video, um, lot to cover.
So appreciate you sticking with it and going through all the examples with me.
Uh, I promise you it'll pay off in the long run.
So we learned about the Bash or Born Again Shell.
You also were introduced to concepts like arrays if and switch statements,
functions, variables. Finally,
you created a guessing game script using all the Bash fundamentals you learned.
In the next video,
you create backups with a bash script and use Kron to automate it. See you soon.