Monday, September 2, 2013

Shell Programming : 9 | Using Loops

Hello.... Friends Today we are going to discus about the looping in the shell scripting loops are basically block of code that is executed until the condition is meeting our need. 
So basically there are four types of loops in shell programming :
  • for
  • while
  • until
  • select
these all have there own functions & there purpose and having there own powers.
So let's start one by one 
  • for loop 
for loop is a loop which does not test the condition variable again & again each time . it just start with the list of items from start to end & test up to when get the result.so for loop is best when you know that what to do or the list is given, the for loop having the do & Done condition. it start with do & finish with done   
for example :
if we want to know the value from a given list then we can use it as :


In this script first we are asking from the user for the list & then as the list is given the for loop start & stop after done
  • while 
while loop as it's name "while" until the condition is true or we can say while loop is a loop which is executed till the given condition is true.
 let we clear it with an example : as if we want to find the multiple of 2 from 1-100, then how we write the script for this, that means all the true condition is displayed the result will be as ,2,4,8,16,32,64, now we know 128 is out of range or out of condition. then lets write a script for this.


while loop is mostly used when no any list is given to us like in for loop we have a list but we have just a condition to test. 
  •  until 
until loop is like as while loop but the difference is only there is no test conditions like while loop so lets understand it more clearly with a script.

here in this script we ask to enter the starting  values as a & b. & we set a until loop here that the string of a is not greater than 15  & string not b less than 200 . this is our loop condition then as we put the value of a is less than 15 is 5 and value of b is greater than 200 is same as 200. then the loop just start & end at only single string.
as we give starting value of a is 10 then loop run up to 15 at one hand & if we put starting value 250 then as our next condition a=$a+1 & b=$b-10 also followed & we obtain the result as soon in the figures.
  • select 
the select loop is nothing but just a loop simply which define the values from a list that what about that value you selected we will discus select with more advance options later , here i am just giving a simple example a list & we have to select one by Number & see what is it's response.



please leave your comments & likes & follow my blog to learn more ...

No comments:

Post a Comment