hey guys back up to the next blog on shell scripting tutorials for beginners and from this blog I will show you how you can use loops in shell scripting and we will start with while loops so first of all what are loops the loop's are used to execute a list of command repeatedly so if you want to execute a command or list of commands repeatedly you can use loops so let's see how we can use while loop and first of all we will see what is the syntax of while loop so the syntax of while loop looks like this so you use the keyword while and in the square bracket you give the condition which you want to evaluate and if the condition is true then this command which are in between the keyword do and done are executed right so you write the keyword do and then whatever command you want to execute and when you are done with the while loop then you use the keyword done so let's take an example and then we will see how we can use loops so what I want is I want to print the number 1 to 10 or using while loop so I am going to declare a variable n is equal to 1 so I am assigning the value of 1 to n variable and then we will evaluate a condition so what condition we want to evaluate we want to evaluate whether the value of n is less than or equal to 10 or not and we have seen this - le flag in the if conditions also right now inside or in between do and done keyword we can execute any come on so what we are going to do is we are going to use echo and then we are going to print the value of n but still we are not done because we are just printing the value of N and the value of n is 1 right so it execute infinitely because this condition is always true right so we want the code to increment the value of n and then we are good to go so how we can increase meant the value of n we can just use n is equal to and then you can just write dollar
and double brackets and then n plus 1 like this and then this will increment the value of n by 1 and assign it to and once again right so let's run the script once again and let's see what happens and it prints the value of N or it prints the number 1 to 10 right now what's happening in this loop so we have initially assigned the value of n is equal to 1 and then once this while loop is reached this while loop evaluates if the value of n is less than or equal to 10 or not and first of all the value of n is 1 which is less than or equal to 10 which is true and then it prints the value of n which is 1 so it prints 1 here and then increments the value of n by 1 so now the value of n becomes 2 and once again it will go at the top and then here now the value of n is 2 which is still less than or equal to 10 so once again this code is executed right and the 2 is printed right in this way this will increment the value of n until 10 is reached and when the value of n is 10 this condition is still mad because n is equal to 10 which is you know conforming to this condition so it will print the value of 4 10 here and then once the value of n becomes 11 and it reaches here so 11 is not less than or equal to 10 so this loop condition is false at this time and then this code will not be executed so once this new condition is false the code will not be executed right so this is what is happening here now the incrementation of variable and you can do in several ways so you can do something like this or you can just use the double bracket to increment the value of n and you can use double + which means post increment right so once again when I run the script it prints the value 1 to 10 once again now you can also do pre increment like this so just to pre increment and once again and we'll run the script and it prints the value of 10 once again 1 to 10 now in here instead of using these square brackets you can also use these double parenthesis as we have seen with the if conditions also so this notation will also work but instead of using this - le flag you now need to use this kind of angle bracket and is equal to right so whenever you are using double parenthesis you need to give this kind of conditions right and then once again when you run the script it per still prints the value 1 to 10 right let me clear the terminal and this is how you can use while loops in bash scripting now in the next blog I'm going to show you how you can for example read the files using by loops and do some other cool stuff with while loops so stay tuned
إرسال تعليق