Check out a free preview of the full Complete Intro to Linux and the Command-Line course
The "Loops & Arrays" Lesson is part of the full, Complete Intro to Linux and the Command-Line course featured in this preview video. Here's what you'd learn in this lesson:
Brian continues adding complexity to the bash script started in the previous segment by demonstrating how to use loops and arrays.
Transcript from the "Loops & Arrays" Lesson
[00:00:00]
>> So, let's make another file. So make another file called friends, and we're gonna chmod +x friends, and then we're gonna vi friends. And then I'm gonna put in here /bin/bash. And then I'm gonna write and quit and open that again, so this is open now in bash mode.
[00:00:25]
And I'm gonna say friends= and I'm gonna put a parenthesis there, which is how you signify to bash that I'm about to create an array. And then I'm gonna put Kyle, Mark, Jim, and I'm gonna put my name in quotes "Brian Holt" cuz I wanna put my full name in there and Sarah, like that.
[00:00:50]
So this is how you do an array, the parentheses and then you just put spaces between them. And if you wanna have something that has a space in it like Brian Holt, you just put that inside of quotes. And then the way you access that is you say echo My second friend is, and you put dollar sign and then curly brace, friends, and then you do square bracket 1, like that.
[00:01:25]
So it's all be the first event using arrays and bash it's a little clumsy in terms of like the how it looks but that's how you do it, right there. And then let's do a for-loop, so we're gonna say for friend in $friends, and then you have to tell it how much of friends that you want and I want all of it so I'm gonna put star in there, like that.
[00:01:56]
And that will give me all of the friends and friend. Then you have to tell it to do something, so I'm gonna say do, Echo friend; friends like this. So that will refer to this friend because it's gonna do like a for in loop, those exist in JavaScript as well.
[00:02:18]
So I'm sure many of you have probably seen that. And then after you do that, you just say I'm done, like that. And then here, if you wanna get the length of an array, this is again, super odd as I have a list. Yeah, I have dollar sign curly and then you put a pound or hash friends and then you have to tell how much of friends that you want the link to, so I want all of them.
[00:02:54]
Friends, like that. And those quotes are probably optional. So as you can see, a kind of a clumsy way of doing arrays. The way that it works underneath the hood is even more clumsy in my opinion. It's actually like converting them to strings and back to arrays and in odd ways, but that's actually how you end up doing it.
[00:03:20]
So let's make sure that this runs, friends, there you go. So my second friend is Mark, which is expect. This looped over and printed all of my friends. You can see it actually split even Brian Holt apart, Which is fun. And then it says I have 5 friends.
[00:03:48]
Yeah, and there is a way to not split Brian Hold apart. The questions I can't remember exactly off the top of my head how to do it. But suffice to say that that is a possibility. But it's actually only storing five things in the array. So like for example, if I vi in that again.
[00:04:07]
I think if I accessed, this would be 0123. See what it says if I do that? Yeah, so it does actually give you that holistically as that. But the for in loop is actually breaking that part by the space. It's kind of wild, right? So I'll just throw out there.
[00:04:36]
I am typically not creating and destroying arrays when I'm creating bash scripts, it's not something that's typically I need to do. I'll consume other programs that use arrays but not necessarily myself. Okay, let's make one more. We're gonna do a while loop and there are more types of loops just as you might imagine, there's like do loops and other things.
[00:05:04]
Just gonna show you two of them and the other one I'm gonna show you is a while loop. And then if you need a different sort of loop, feel free to go look for them. So let's make another one called guess. And let's actually touch guests chmod +x guests and vi guess and we'll insert our hash bang in bash, right quit and oops, now we can write this.
[00:05:44]
So, the first thing we create is a number to guess, num to guess. I'm gonna create this variable. And then we're going to do dollar sign parentheses, parentheses. Dollar sign random, which $random is always gonna be a random number between zero and things like 32,000 or something like that.
[00:06:08]
It's like 2 to the 12th divided by 2 or something like that, that's some specific number like that. $random and then you can do modulus 10 cuz we want it to be between 0 and 9, right, which is what the modular does, it's like the remainder of a division, right?
[00:06:32]
You've probably seen that in other languages. And then after you do that, we're gonna do + 1 cuzwe want it to be between 1 and 10. Let's talk about the double parentheses for just a second. Do you remember like when we were doing the if statements that converts that to test the double parentheses with the dollar sign converts to, which one is it, let.
[00:07:01]
It converts to another program called let, and let let's you do basic arithmetic. So when you do these double parentheses, it's like, hey, I'm about to do some math. And you're gonna give it some numbers and then expect some number back from it. So that's what this is doing this versatile to a let which allows you to do modulus.
[00:07:20]
Because modulus does not typically work or not not modulus, modular. Modular does not work in bash script, you have to pass it to let and then get that number back. Okay, so suffice it says after this num to guest is gonna be a random number between 1 and 10.
[00:07:40]
Then we're gonna get a guest, Num. And that's gonna be initially equal to 0, which is gonna be outside of the possibility. When I say echo guests a number between 1 and 10. And then I'm gonna say, while, $NUM_TO_GUESS is not equal to, GUESSED_, NUM, Do, Read -p your guess, GUESS_NUM.
[00:08:35]
Done, then after they're done, they're gonna say you got it. All right, so let's break this down just kind of bit by bit. Number guess, this is gonna be some random number between 1 and 10. This isn't gonna change, this is only get set once at the very beginning.
[00:08:52]
And then guess number is 0, which is not a number between 1 and 10. Okay, then we're gonna echo guess a number. And the first time this runs, obviously zeros outside of the boundaries, so it's always gonna run at least once, okay? And then this square bracket notation should look familiar too, this is also works with test, right, so the same with it.
[00:09:11]
If statement works, this also works for while loops as well. So this is going to test every single loop is the statement true. And then eventually, when num guess is equal to guessed num when they've actually guessed the number correctly, this is gonna be false and it's gonna break out of the loop, that makes sense?
[00:09:31]
Okay, and then here in the middle, we're gonna be reading from the user, numbers. It's gonna pause here until this happens, and then it's gonna run the loop again if they don't guess the correct number. Eventually, this is gonna finish once they guess they correct number, and it's gonna echo, you got it, and then it's gonna be done.
[00:09:51]
So let's see if it works. Guess, so it gets a number between 1 and 10, 1, 2, 3, 4, the answer's 4. That one's a little bit more opaque. So I'm just gonna make sure that we got it. This read statement here is gonna pause. It pauses the execution of the entire bash script until it happens, right, which is why this doesn't keep looping indefinitely.
[00:10:15]
But as long as you did not guess the correct number, this would loop indefinitely. It loops until this is not true. Here on this line, why is there no dollar sign for GUESSED_NUM? And the reason why, you're giving guess num to read. So this is actually an argument that's being fed into read and then read is using that to set a variable for you, right?
[00:10:40]
If I put the dollar sign guess num, what would happen? It would actually put zero here. So the thing that read would receive would be 0, it would be the value of this. So cuz whenever you do the dollar sign there, bash is gonna go and replace it for you before it gets made fed into the program.
[00:10:56]
We don't want the value of guess num, we wanna string guests num fed into read so that it can set that variable for us. Does that make sense? Yeah, that's a great question.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops