hello guys there will be the next Blog on shell scripting tutorial for beginners in this Blog will see how we can use the if statement in our script now if you are familiar with any other programming language you may know that if statement can be used to evaluate some condition and if the condition is true then your you know code will be executed otherwise it goes into else condition or it's not executed right so let's see how we can use if statement in our bash script so to use if statement in bash script you have the basic skeleton like this so you write if and then in square bracket you write some conditional expression okay so I'm going to just write here condition and then you write a keyword called then and then you if the condition is true then you perform that statement and then if the if statement is you know done then you write fi for the end of this statement right so this is the basic syntax of if statement let's see how we can use it with some example what I'm going to do is I'm going to declare our variable let's say the variable name is count here okay and the value of count is four examples 10 okay and we want to evaluate this variable so what I'm going to do here is in the square bracket I'm going to just write the name of my variable and then there are some comparison operator here I can use with the integer so for comparison what you can use you can use equality or non equality or greater than or less than or greater than and equal to or less than equal to right so these are the comparison operator you can use so I'm going to show you the list which are the operator you can use with integer so you can see this is the list I have so you can use with integer - EQ 4 equal to minus n e4 not equal to then GT for greater than GE for greater than or equal to LP for less than and le for less than or equal to and if you want to use these angle brackets then you need to use doubles parentheses instead of this kind of square bracket so let's use our first comparison operator which is equal to right so after this count you just need to write EQ for equality and let's compare it with for example nine right which we know that this condition is not true because 10 is not equal to 9 right so what we can do here is we can echo that this condition is true or false right in our case this is not true so nothing will be printed right so let's run our code and let's see what happened so I'm going to run our script and prints nothing because 10 is not equal to 9 right but if I just use 10 here which is the true condition then let's see what happened so it print condition is true right in a same way we can use all these expressions which I have shown you so for example ne4 not non equality so any and let's use the 9 here which is a true condition because 10 is not equal to and 9 right and once again going to show us condition is true okay and so on so if you want to use greater than just write GT here GT so 10 is greater than 9 which is true and you will be able to see the result once again right so all these operators you can use to evaluate your condition in the case of integer let's see this angle bracket case also so for checking whether this count variable is greater than 9 or not we can just use this kind of angle bracket and wherever the angle bracket opens it has to be a bigger number in order to fulfill the condition so let's see what what is the result of this script and it feels true and if you want to just run it with double parent this is also this expression you can just use it double parenthesis and then run it then it says true also right so this kind of expression you generally use with double parenthesis right and you have other expressions also like greater than or equal to or less than or equal to so let's see this expression this is also true condition so you will see your you know result here now for comparing the strings you have to use for equality single equal to or double equal to they are same and if in the case of non equality you can use this kind of exclamation marks and equal symbol or you have the angle bracket for you know checking the alphabetical order okay so let's say our for example we have the word and let's say it's ABC right and here we can just use the square bracket once again and then we can just write in the name of our variable which is word and then to check whether it's equal to whatever we are providing here so for example we say ABC let's check whether this condition is true or not right so let's run the script and sales condition is true now let's change this comparison for example some random keyword and you can see there is no result that means this string is not equal to the string which is in the board of variable right so for string comparison you use single equal to single equal to will also work here so for example I have this and single equals and then I run the script once again it says true so you can use for equality single equal or double equal which is they do a little bit weird when you compare it with some other languages for example C or C++ or Java or any other language they use this double equals for equality but in our case you can use single equals or W column script right now you can also use not equality or non equality for string like this so for example I have this string and this condition is also true so whenever we run the script it says condition is true okay and whenever you want to you know check the alphabetical order then also you can use at these angle brackets so let's say this is a and here we have the B right and let's run the script once again let's say what happens so it says no such file or directory so as we have seen in the case of for integers whenever you use these angle bracket you need to use double parenthesis right so whenever you use angle bracket with strings you need to use double square brackets that's why you see this error here which says no such file or directory right so you just need to use double square bracket with good string whenever you use this these kind of angled brackets right and let's run the script once again and this time hopefully it will run so it runs so keep these you know conditions in mind so whenever you want to use the comparison operator with integers you can use single square brackets which gives EQ on any on GT operations whenever you want to use these angle brackets on integer comparison you need to use the double parenthesis whenever you want to compare strings you can use single equal to or double equal to for equality and whenever you want to use angle bracket with strings you just need to use double square brackets okay now there can be some other conditions with if you can use else condition also with if so let's see how we can use as condition so you may have guessed you just need to write else here so just write else and then you can just say for example condition is false so if you want to use this kind of expression you already may have guessed that if the condition is true then it is going to go to this block after then before else and if the condition is false then it's going to go to the else statement and whatever is written between s and the end of the if or condition that code will be executed right so let's run the code once again and let's say we say that a is equal to B which is false so else condition will be executed right so it says condition is false so if the condition is not true then the L statement code will be executed right now there is another way you can use FL condition so if you want to evaluate multiple condition and on the base look for those multiple conditions you want to execute your code then you can use if/elif/else condition so for example I'm checking here if the variable is a or not so for example I can here also write if and then if the condition is true this statement will be executed otherwise I can also write here else if condition so just write L if and inside LF once again write them and then here you can also write some statements so for example you can just write something like this so whenever this condition is true we will print condition for example B is true here and then here we will write condition a is true and here we need to write some one again some condition to check so with LF also you can use these kind of conditions and then for example we will just say a here right because we are searching for a word is a and this will be acquainted right so once again run the script and let's see what is the result is and you can see condition a is true which is this one because our word variable is equal to a which is this condition right so if this condition is false then this condition will be evaluated and if it's true then this code will be you know executed otherwise the last option is this option in else condition so in this way you can use if-else condition if condition and LF condition in your script so I hope you've enjoyed this blog
Post a Comment