hey guys welcome to the next blog on shell scripting tutorial for beginners in this blog we will learn how to use for loops in shell scripting now if all loops are also used to loop over some lists of values and then execute the command in the loop now let's see first of all what is the basic syntax of for loop and then we will see some examples so there are several syntaxes we can use with carloads and i'm going to show you some of them so these are some of the basics in texas we can use in the paw loop so the first syntax here is we use the for keyword and then we declare a variable and then we use the keyword in once again and then we gave the list of values it can be for example 1 2 3 4 5 or it can be in the list format which we will see a little bit later when we will see the examples right you can even give the files as an input here right so file1 file2 Oh file 3 and we can even give some Linux or UNIX command here as an input ok and then we have the do and done keywords in between we execute some commands right now the next way of using the for loop is similar to as we have seen in for example in C programming so we use 3 expression in between these brackets and the first expression can be used to initialize the value second is to compare or if the condition is fulfilled we check this in the second expression and in the third expression for example we can increment the value which we have declared in the first expression right so this is the other way of using for loops right so let's take some examples to understand the for loop in a better way so let's take the most basic example so for example I want to I create some numbers and I want to print them so I can just write the numbers like this so whatever number I want to you know iterate on I can just give the numbers separated by the spaces so for example I want to give five numbers then I can give these five numbers here one two three four five right and in the do and done keyword in between these two keywords I can echo the numbers using the variable so I can just use the dollar symbol here and instead of variable I can declare any variable for example I or any variable name whatever you want to give yes for example I use I here right and that's where you'll be like in print from here okay and let's run the script and let's see how it works so I will run the script and it prints the number one two five pretty simple right now in case of longer ranges for example you want to iterate over hundreds of values this method is not you know very beautiful because you need to write every number and then iterate over it right so if you have bash version greater than three then you can use this kind of notation also so you can give any range in between these curly brackets so for example I want to iterate over one to ten I can just write one and then double dot and then ten here and this means that we want to iterate over the range of one to ten okay and then once again let's run the script and let's see what happens and you can see the result is the printing of one to ten numbers right so this is how also you can give any range between any numbers right now there is one more way of using this range so this will work f-from one to ten so this is the starting value and this is the ending value and then you need to use two dots here now if you add two more dots then you can give the increment by which you want to increase the value for example I want to increase the value by two every time this range is looped over then I can write to here right so basically what this range means is the first value here is the start value then double dot and then second value is the end value and then the third value of the dot is the increment we want to give so I want to give the two increment right so let's run the script once again and let's see what happens and you can see it start with one because our initial value is one and then the value is incremented by two whenever we loop over this for loop so every time we loop we increment the value by two right if you want the value of starting from zero you can do that also so let's run the script and now we have the number zero two four six eight there now also you can do this but remember this kind of notation you can use with the version more than 4.0 or bash right now you may ask how we can you know print or how we can know the version of bash we are using this is pretty simple so you can just echo this by using a keyword called bash version this is the keyword which you can use to print the version of bash so once again I run the script and
I can see I have the bash version 4.3 point 11 right so this notation will work on my you know bash script but if it's lesser than 4 then this notation may not work in your case and if you are using this notation then you need to have minimum version three or more okay so I have four point three so both of the notation works with my bash script now let's see the other way of using for loop in scripting okay so instead of this notation I told you we can use these double bracket notation and in between we can give three expressions right so let's declare for example I and initialize it with zero right and then by semicolon we can separate these command or expressions and then in the next expression we will check whether this value of I is less than five or not and then we can increase the value of I by one every time we loop over this loop right so this is the first expression in which we are initializing the value of I with zero in the second expression we are comparing or evaluating this expression whether I is less than five or not and then we are incrementing the value of I as we do in normal programming right so you already know how to do is so let's clear the terminal I'm going to clear the terminal and run the script once again and you can see it prints from zero to five right because the value of I begins from zero and it evaluates whether the value is less than five and if the value is equal to five or greater than five then this will not be executed okay so this is how also you can use the for loops now in the next blog I am going to show you how we can use for loops with files and commands so stay tuned and i hope you enjoyed this BLOG 😊
Post a Comment