Shell Scripting Tutorial for Beginners 8 - Logical 'AND' Operator


hey guys welcome to the next blog on shell scripting tool for beginners in this blog we will learn how to use end operator with a statement so let's get started so suppose we have a student and we need to evaluate his age and we need to evaluate the age in such a way that his age should be greater than 18 and less than 30 years so how do we do it so let's say we have a student whose age is 25 right and then we will write an if statement so I'm going to write a if statement and then as we do with if statement we will just write the condition so we can write here age so dollar age and then we can say if age is greater than gt4 that with minor right so greater than 18 and we need to write the second condition also so for that we can use and operator and there are two ways of using app and operator with if statement in bash script one is to use these kind of four double and symbols and then make a second square bracket and then give our second condition here so the second condition is dollar age if the age is less than minus L T less than thirty right so we are evaluating two conditions here if the age is the greater than eighteen and the age is less than thirty then and then we will say success or fail so we will just echo valid age otherwise we will echo so let me write L and then we eko age not valid right and then we will end the eight condition by FY so whenever you want to evaluate more than one condition you can use two kinds of operators one is and operator and other is all operator which will be we will see in the next blog right so in this case we want to check whether the age is greater than 18 and if it's less than 30 at the same time so we use end operators and if you know little bit of programming you already know that and is for checking or validating both of the condition so if and only if both the conditions are true then only this code will be executed otherwise this code will be executed right so let me run the script so I'm going to go to my desktop folder so CD desktop and then I'm going to run my script and you can see age not valid right and why it's not valid because I think I forgot this space between 30 and the ending square bracket so we will run our script once again and this time it fails valid so always remember to give the space here and here okay so now let's give the age which is out of the range for example 50 year and let's see what happens so I'm going to run the script I'd say is age not valid and what's happening here the first condition is true because the age is greater than 18 so this condition is true but the other condition is false the age is not less than 30 right therefore the whole condition is false right in order to pass this and operators you need to pass both the condition this condition and this condition also right now there is a second way of using and operator and this way maybe is for some people it can be easier because it only requires one square bracket and not two square brackets so what we need to do is we can just remove these square bracket from here and then we can use a - a flag which stands for and okay so if you want to just use one square bracket then for an operator use - a flag and the condition or situation will be the same so - a stands for and operator and let's run the code once again and it's a age not valid and let's give the valid age and once again the age is valid now there is one last alternate way of using and operator with your if statement and that is you can use these double and operators like this but you need to use the double square brackets like this for that okay so if you want to use double square back brackets also you can use this kind of operator and it will also work fine so let's run the script once again it saves each valid and let's give the invalid age and let's run the script once again it says age not valid so there are three ways of using and operator one is this wave with two brackets and this and operator if you want to use one bracket then you need to use - a here and if you want to use two brackets then you can just give two conditions separately in two square brackets so in this way you can use end operators with if statement in scripting I hope you enjoyed this blog.


Post a Comment

Previous Post Next Post

Recent in Technology News