 Today, we're going to be writing a script using if-then-and-else statements. This is a statement where you can get a user input and then compare it to possible choices. So in this case, we're going to be working with integers, which are numbers, whole numbers. So basically, we're going to get a number from a user, and then we're going to check if it's this number, do something else. If it's another number, do this. If it's neither of those numbers, do something else. So let's jump in. I'm going to use VIM as my text editor. There's no need if you don't like VIM to use it. Text editor is a personal choice when writing scripts. You can use VIM, nano, g-edit, vi, k, k-write, something like that. Just make sure you're not using a word processor like Libre, write, or like Microsoft Word or something like that, because those are not text editors, they are word processors. So make sure it's a text editor. And I'm going to call this my script, and I'm going to put it .sh, even though that's not necessary. Unlike a Windows operating system, Unix and Unix-like operating systems. So Linux, BSD, Unix, macOS, the extension on the script has nothing to do with what the computer is going to run it as. What is going to tell the computer what type of script it is, is the shebang line, which is the first line in your script, and should always be the first line in your script, whether it's a shell script, Python script, Perl script, the shebang line, pound, exclamation mark, in this case, bin and bash. It's just telling the computer that this is a bash script. If you're writing a script for another type of shell environment, instead of bash, you would put that environment there. But the first line telling the system this is a bash script. And then we're going to have some output. I'm going to say echo, and in this case, dash n, because I don't want it to print a new line at the end of this echo, just for the look and feel of the script, it may depend on what you're doing. I'm going to say enter a number. And then the next line, I'm going to run the read command, which waits for a user input and for them to enter, but we want to save the input that the user gives to a variable. I'm just going to call it num for number. So we're asking the user for a number, and then we're waiting for them to enter one and hit enter. And in this case, we're going to save that, what the user inputs as the variable num. At this point, we start our if then statement. So what I'm going to do is I'm going to say if, space, and then square brackets. And I'm going to say dollar sign, num. Inside quotations, because we're looking at our variable num. And in this case, we're going to say dash eq for equals. And then we're going to give it a number. I'm going to say 10. Oops, I should have number lock on. Let's see. 10. Now, it's important that you space this out, right? So make sure there's a space there, space there, space there, and space there. So next I'm going to say then, which means if this happens, then do this. And I'm going to go ahead and close my if statement with fi, which is if backwards. So now what we're going to put in here is what's going to happen if you enter the number 10. So we're going to say in this case, we're going to say echo, that is a good number. And I will save my script and we'll run it. But before we run it, since we just created it, we have to make it executable. This is a security thing on Unix systems so that you can't just download a script or a program and have it automatically run and infect your system. You have to, as a user, give it permission to run. And by giving it permission to run or execute, we're going to say change mod plus x and then the name of your script. Now this is usually where someone goes, well, you don't have to make it executable. You can type in bash and the name of the script without making it executable. That's true. But still, this change mod of making it executable, a script can't download and run itself because it can't run the bash command until it's executable. So this is, once again, just for security, even though you could run it as bash and then the command, that's something the user has to physically do unlike other operating systems where you download an exe and it's ready to run regardless of whether you give it permission or not. So that is why we have to do that. You only have to do it once unless you move the shell script to another system and depending on how you move it, you may have to make it executable. So anyway, once it's made executable, since it's in the folder we're in, we're going to say dot forward slash the name of our script. So I'm just going to start to type it. I'm going to hit tab. Once again, tab will auto complete and if we hit enter now, it says enter a number. First I'll just hit enter three and we'll hit enter and we get no output. But if we were to run the script again, I hit up arrow and then enter to run the same command again. This time I'm going to type in 10 and hit enter and it says that is a good number. Why? Because that's what our if statement said. So let's go back into our script here and we're going to say, we're going to look at it just real quickly here. We've got where it asks the user for a number. We get the number that the user inputs and saves as a variable num and we're going to say if that variable equals 10, do this. And that's the only thing we have in this script. So if it's anything other than 10 at this point, it is not going to display anything. It's got to equal 10 to get that output. Now we can also put in an else part of this statement. So we got if it equals 10, do this. Else means if it's not 10, if it's anything else, what are we going to do? We're going to say echo, I don't like that number very much. So let's run that. I saved it. I don't have to make it execute again because the script is already executable. And in this case, once again, we'll type in 10 and it says that is a good number. But if we run the script again, and let's say we put in 12, it says I don't like that number very much. And any number other than 10, you're going to get that sort of output. And if we put in letters, we actually get an error. And that's because in this if statement here, we are checking for integers only. So if they enter a string, some characters, some letters, you're going to get that error. Will in the future tutorial go over how to check that so that your script doesn't get errors. It will maybe re-ask them the question and tell them that the input they gave was invalid. But a little more to this tutorial, we've got our if statement with our one option and our else. But let's say we want more than one option other than 10. We can then say LF, so else if basically. And just like before, inside square brackets, we're going to check the value of num. And if it equals, we'll say 20 in this case, then echo that is one good number. So if we save this, and if I typed everything right, if we type in 10, it says that is a good number. If we type in 20, it says that is one good number. And if we type in anything else, as far as integers, numbers, we get, I don't like that number very much. And you can do more of these. So if you wanted another option, you just do another LF, check the value of your variable to see if it equals, we'll say 15 in this case, we'll say echo another good number. So in this case, if I type in 15, I typed something wrong. Oh, I forgot my then. There we go. So else if then, sorry about that, 15, then it says. Another good number. So once again, I'm going to hit control L to clear the screen, which is actually something we can throw into our script. I've gone over, if you type in clear, it will clear a screen. And I'm going to, before our if statement here, I'm going to clear the screen again. That's something I like to do just to keep things clean and clear. So entering number, we'll say 10, it says that is a good number. Run it again. We'll say 15. Another good number. Run again. Check 20. That is one good number. Run it again. Type in some other number. Then we get, I don't like that number very much. So that's just the beginning look at if statements. Obviously, you can check things other than just if it equals something, which is what we'll go over in the next tutorial. I thank you for watching this tutorial. Please visit filmsbychris.com. That's Chris with a K. There should be a link in the description. We've got plenty of tutorials there, some beginner stuff, some advanced stuff, stuff on other than shell scripts. If you enjoyed this video, if you enjoyed these topics, give this video a like. Be sure to subscribe so you don't miss out on any of my future tutorials. I hope that you have a great day.