hey guys welcome to the next blog on shell scripting tutorial for beginners in this blog we will learn how to use until loops in shell scripting now until loops are almost similar to while you but with a slight difference and the difference we are going to see in a moment but let's see first the syntax of until loop so the syntax of until loop is like this so you use the keyword until and then in square brackets or in double parenthesis also you give a condition and if the condition is false then the commands between the keyword do and done are executed right so the difference is in the while loop if the condition is true then only the commands are executed in the until loop the condition is false then only the commands are executed right so let's take an example so once again for example I want to print the value one to ten on the terminal so I will declare a variable and assign a value of 1 to it and in the condition this time I want to write here dollar n and then I will just give it a flag GE which is greater than or equal to right and then 10 so we want to evaluate the condition if the value of n is greater than or equal to 10 and if it's not greater than or equal to 10 then it's going to echo the value of n for example right so we will just write like this and to increment the value of n we can just write this notation so just write and is equal to double parenthesis and then n plus 1 here okay so let's run the script and let's see what happens so I am going to run the script and it prints the value 1 to 9 and why it's happening here because this condition is false whenever we start our script n is equal to one here right so when this point is reached the N is equal to 1 which is not greater 210 or not greater than or equal to 10 so it goes here right so it's checks for the false condition right and then if the condition is false which is false in this case because n is not greater than or equal to 10 then it prints this value with the echo command and then increment the value of n by this right and it goes on and on and on until the value of n becomes 11 which is greater than or equal to 10 which is true so if the condition is true this is not executed right now
if I want to print 1 to 10 I need to just write greater than here not greater than or equal to what's greater than right once again when I run the script it prints 1 to 10 right similar to a while loops you can just use these double parentheses here to evaluate the condition and we will just do something like this but instead of greater than you just need to write something like this so until the n is not greater than 10 then we will execute the command so once again the values are printed 1 to 10 right and as we have seen we can increment the value of n in few ways so the post increment is possible let me clear the terminal and once again run the script and the result will be same and pre increment is also possible so like this and once again run the script and give us the similar result okay so in this way you can use until loop in shell scripting I hope you enjoyed this BLOG 😊
Post a Comment