 Today, we are going to be looking at if-then statements. This is how you check the value of something. So in the previous videos, we talked about getting user input. Well, what if the user doesn't give you input? You want to do something different than if they did. Or let's say they give you a value, like a number. And you want to do something, whether it's equals a number is greater than a number is less than a number. We're going to look at that today. So this is another thing that you're going to be doing a lot in scripts. Because, again, you're going to be getting input and displayed to the screen. But what you're displaying is based on what you get. So let's go ahead and jump right in. Again, I'm going to be using Vim as my text editor. We are working with Bash as our shell. You don't have to use Vim. Vim is a great text editor. I plan on doing videos in the future. Use whatever text editor you like. So again, let's go ahead and jump right in. So I am going to use Vim to create a file. I'm going to call it script.sh. And we want to start our script with our, again, shebang line. This is telling our operating system when we run this. Because as far as the operating system is concerned, this is just a text file. We have to tell it what program, the interpreter, what program to read this file line by line. And we're looking in this case at Bash, which is in your bin bash directory. So in the past, we talked about the read command dash p. I can say something like, enter your name. Name's name. And then we'll give it name. And then we will echo, hello, dollar sign name. We'll save that. And since we just created it, if you just created it or downloaded it, you're going to one time have to make it executable with change mod plus x and the name of your script. At this point, I can say dot slash, which means we're running a script in the current directory. That's what dot slash means. Dot means the current directory dot slash script. And when I run it and ask for a name, I'll say that I'm Chris and it says hello, Chris. But what if I don't give it a name? What if I just hit enter? It says hello, which I guess is fine. Still saying hello. But in certain cases, you want the user input something and if they don't or if it equals something, we want to do something different. So let's go back into our script. And what we're going to do here is we're going to say if, and I'm going to say dollar sign name, then if. Okay? So now what this is doing, and it's very important you have these spaces here and here and here. If you leave out spaces like this, you can even see, because I got color coding here, something's not right. So you want to make sure that you have spaces proper. Bashes is particular about that. But if you just give it a variable, it's going to check if that equals anything. Okay, so here, if it equals anything, it's going to say hello name. If not, it's just going to continue with the script. So I can run my script again. I can enter Chris and it's going to say hello, Chris. But this time, if I just hit enter, it's not going to say anything. We can go back into our script and then we can echo goodbye. Now if I run my script, it's going to say answer your name. I'm going to say Chris. It's going to say hello, Chris. Goodbye. And if I don't give it a name, it's just going to say goodbye, okay? But how about this? What if we wanted to say something different if you don't enter something, not just continue with the script? What we can do here is we can say else. And this is, if all the previous if arguments fail, this is what we're going to do. We're going to say echo you did not enter a name. And let me go ahead and just do that. Now, if I run my script, I'm going to say Chris. It says hello, Chris, goodbye. But if I run it and I don't give it a name, it says you did not enter a name. Goodbye. Now we can check for multiple things. So what I can do here is I can change this. And I'm going to put it in quotations because we're checking a string. I'm going to say equals equals. And in here I'm going to say John. And then what I'm going to say is then echo, it's John exclamation point. One exclamation point. Remember, we're a previous one. If you do two, you have to do some special things to allow that. And then we're going to say L if. And then we'll just say dollar sign name again. So what is happening here? We're asking you to enter your name. It's going to wait for you to enter a name. Whatever you enter, it's going to put in the value of name. If the value of name that you just entered equals John, and it is case sensitive, it's going to say it's John. If the name equals something, it exists. It's going to say hello, your name. If you enter nothing, it's going to say you did not enter a name. We will go ahead and save this. We will run it. I will type in Chris. It says hello, Chris, goodbye. If I enter nothing, it says you did not enter a name, goodbye. If I enter a name and I enter John, it's going to say it's John, goodbye. But again, it's case sensitive. If I type in John like so, it's going to say hello, John, not it's John because it doesn't recognize it as the same name. I'm going to hit control L to clear the screen. I'm going to hit up twice. Remember, you can go through your history that way. I'm going to say Vim again, my text editor. It is important that you do things in the correct order. So if I copy this and put it here and copy this and put it here and change this to if and I say L, if. So here it's checking, does the variable exist? Oh, we should change these two. Just moving lines around. Here we're checking, does the name exist? If so, say hello name. Then we're checking, is the name John? If so, then say it's John. If not, say this. So now if we save this and we run our script and I type in Chris, it's going to say hello, Chris. If I do nothing, it's going to say you did not enter a name. And if I type in John, what do you think is going to happen? It's going to say hello, John. Why is this? Why does it not, here we specifically say if it's John do this? Well, once it gets a value that is true, it does not continue with the if statement. So it's saying, okay, did you enter something? Even if it's John, print this, ignore the rest of this. So with if then statements, it's very important that you do things in an order that makes sense when you're checking them. Now we can do some other things here. Let's go ahead and change this to enter a number. And here we'll just change this to num. And here we'll say num. And we're going to say, okay, if num equals three, well, then we're going to echo you guessed correct. And in anything else, we're going to say wrong. Now, if I run the script, if I typed everything right, enter a number, I'm going to enter five. It says wrong, goodbye. I enter three. It's going to say you guessed correct. If I enter nine, it says you guessed wrong or any other number, it's going to say wrong. So let's go ahead and look at this again. So we're checking, is it three? Okay, let's add something to that. Is it three? Okay, if not, we're going to say L if, remember all our spaces are important here. We're going to say greater than three. Well, then we are going to echo too high. Too high, L if, dollar sign, num is less than three. Then why did I put that on a different line? Dollar sign, num is, oh, sorry, echo too low. And I realized I put the wrong two in here before the grammar Nazis say something. Too high, too low, let's run this script. Enter a number, I'm going to enter five. It says too high. Enter a number, one, too low. Enter three, you guessed correct. Control L to clear the screen or you can type clear. Now what happens if I type something that's not a number? Too high, so it is looking at that as a number value. So that's important. Now also look at our script. We will never get to the else wrong option here. So if we run this and put nothing, it still says too low. Some people say you should always have an else at the end of your if, then statement. I can't think of a case where we're actually going to reach this, so I don't know. Maybe I could rewrite this in a different way. Again, we could also say something at the beginning to make sure you entered a number because it's weird that it says, if I enter nothing, that it's too low. So I could always change this to L if and I can say if dollar sign num, what I can do here. I think this will work. There's different ways you could do this. It's probably a better way. Then echo you didn't enter anything. So I'm kind of making this up as I go along. If I type in one, too low, six, too high, three, correct, nothing, you didn't enter anything. So I'm assuming there's probably a better way than saying is the string empty this way. But just giving you options here, that's all I kind of wanted to go over. We're going to go over shorthand for if then statements in the next video. So, and also checking the value of the output of commands. I do thank you for watching. I hope you found this useful. If you have any questions, comment below. Be sure to like, share, comment, subscribe, all that good stuff. And as always, I hope that you have a great day.