 Hello everyone. In this video, we'll be discussing about the loops in shell scripting. Here is the learning outcome. By the end of the session, students will be able to implement loops by using shell scripting. So this is the outline where I'll be first of all introducing you by taking a couple of examples of the general programming language conditional statements and then I'll be demonstrating you with the help of example. So before proceeding ahead, it's a good moment to discuss about what is meant by conditional statement in a computer program. You can pause the video for a while and think on this and then you can resume the video to find the answer. So this is what we have in the form of a flow chart that is available on the screen where a conditional statement generally has a sort of condition and generally as we are aware that a condition would be either true or false. If it's true then we can put a certain piece of code that would loop continuously until the condition is met. So that's how it is represented with the help of this simple flow chart. So in a computer program, the code begins then we write a condition like whether a specific content is equal to x, y, z and if it is equal then a piece of code is executed and if it is false then either we can end the code execution or we can write another piece of code which would indicate the user that the condition is false. So it is always a choice of the developer like whether to allow the code to be looping until the condition is completely true or a specific condition is met. So you can either go with the active high kind of a condition looping or you can even go for active false of a condition looping. Further you can even write a piece of code under the false condition which will allow the user to execute these set of statements when the condition is completely false and then ultimately we end up with the code execution. Now to be more specific about the shell scripting as you can see we have the following types of loops available in shell scripting. The first one is if else which is just a simple single condition based looping statement or a single conditional statement where you can only mention a single condition. Another is whenever you want to write multiple if else, if else, if else like what we generally do in C language and other programming languages then here the structure is if and lf and then it can be continued further until you completely end up the loop by writing its end determination keyword called fi and next we have loops the for loop, the while loop and until loop. I'll show you a couple of these by showing a set of demonstration. So let's have a look at the demonstration now. Okay as you can see I have logged in into the U1 to terminal. Now as usual let's make a small directory like I'll be calling it v5. Let me first of all make a folder called v5 then I'll enter into that and let me show you that I don't have anything currently present inside this. So let us go for creation of a file called iftest.sh. So I'm trying to open a new file called iftest. Now let's follow the general process of mentioning the shebang. This is the shebang then we have let's begin with writing the shell code for demonstrating the if loop. I'll write a simple program. Okay let me first let me write something which would prompt the user to enter a number. So this statement is very simple where it will prompt the user to enter his age then I'll try to read this in a variable called age. Now I'll put an if condition now you can have a look at the syntax. Now let's have a look at the basic if loop syntax that is you need to simply write an if everything in lowercase then put a square braces then inside this make sure that you put double spaces. I mean to say that whatever you type inside this it should not touch these braces so that's the syntax if your internal content of the bracket of your if condition is touching these braces then it is going to invoke some sort of errors. So you need to be very disciplined as far as these conditions of writing if loop is concerned. So I'll read the age that the person is trying to enter and I'm going to try. So unlike the symbols that we generally use in C language like this we need to write something like a greater than or equal to for example I want to simply assess whether the user entered age is going to be greater than or equals to 18 and then I'm going to output a statement or print a statement to the console stating that whether he is capable to vote or not. So that's what I'm currently trying to program. So I'm putting this GE to indicate that with the age content is greater than or equals to 18 and then I simply put a semicolon and then I can write then so if this is true then I'm going to print to the console like you are eligible to vote under else I'm going to put you can't vote. So if the user is trying to enter any age value less than 18 or any value other than 18 especially on the lower side on the lower limits of 18 then it should probably print you can't vote. So let's quickly check I'm going to save this. Now as usual we need to first of all change the mode of this file. Now make sure it's displayed in green which is a meaning that it is going to be an executable. Now I can simply put a dollar slash and sorry dot slash and if test dot s h. It's giving me an error stating that bin slash bash. So let me quickly change this. I forgot to mention the slash. So it's asking me to enter the age for example if I'm writing some value like 25. It says unexpected let me check what is the error. An expected end of line. So it is very important to understand what the error exactly says. So it is saying that the file called if test dot s h is not properly terminated. So let's go back quickly and let me indicate what error we have done. So the error is we have written that if we have written the else condition but we forgot to end the file. So remember if must be ended with the reverse of if that is fi. So whenever it finds the fi keyword it is going to identify that it's the end of your reflux. So let me quickly save it again clear the terminal and sorry if it's asking me the age let me enter 25 for example. It says yes since the condition is met that like 18 is less than 25 or 25 is greater than 18 it states that you are eligible to vote. Similarly if I execute it again and if I'm writing 8 it should display you can't vote. Yes so that's how we we have successfully verified the working of if else. Now let me demonstrate you something else. I'm going to write or modify the same program into something else. So let me say I'm having three colors red, green and blue. I'm having three colors red, green and blue. I'm going to prompt the user to select red or green or blue. So I'm going to name this variable as color. I'm going to read this color and I'm going to compare this time I'm going to compare strings. So I have possibly selected this example so that you can understand how to write these things for strings as well as integers. Make sure you maintain the space between the bracket and the equal to sign. So whenever I'm comparing strings I will be using a double equal to sign and I'll be comparing and I'll be comparing red if the color is red. I'll say you selected red. Now I want to have another if condition means I want something where I can mention a condition which was not possible to be mentioned under the else condition. So that's the reason why we prefer writing if L if. So the only change in else is going to be this. I'm going to keep it as it is and I'll be going for ELIF which stands for else if means it's a recursive if else kind of a process where you can mention another condition. Again follow the same condition like I'm going to read color and I'm going to compare it with whether I have selected green or not. Make sure you're maintaining proper spaces between this parenthesis and then I put a semicolon then write then and you can echo whatever you want here. I'll be writing your your selected color is red. Similarly here I mean right if it's true with the green condition it will be displaying me your selected color is green. Otherwise means for the third condition which where I'm not mentioning any sort of condition that is anything other than red and green is going to be the blue color. So let's quickly save this and check since I have the same name given to this one. Yes so it is showing me that I have selected the green condition. If you see the code you will understand that I have typed green here which is a meaning that my L if condition has become true. Now let me try to execute it again by this term I will write something else. Let me call ABC. I have selected ABC then I want the else condition to be said true. Yes so as you can see I have typed anything other than red and green. It's a meaning that that ABC is coming under the else condition. Here are the references used for this video. Thank you.