Shell Scripting Tutorial for Beginners 22 - Break and continue


hey guys welcome to the next blog on shell scripting tutorial for beginners in this blog we will learn how to use break statement and continuous statement in shell scripting so let's start with the break statement so the break statement is used to exit the current loop before its normal execution so whenever we want to break out of the loop prematurely then we can use break statement so let's take an example so for example I have a for loop like this which initialize the value of I as one and then we are awaiting the condition if the value of I is less than or equal to ten and then here we are just incrementing the value so this construct you already know I already told you in the you know last blogs right and then I simply just you know print the value of I and let's run the script and let's see what happens first of all so I'm going to just run the script and it prints one to ten which is a normal operation now for some reason we want to exit out of the loop prematurely for example if the value of I is greater than five then we want to come out of the loop then what we can do here is we can use for example if and then inside the if statement we can say if I so dollar I is greater than five then we want to come out of the loop so then and then fi for to end the loop and in here we just give the break so just write break keyword here and what it does is whenever the value of I goes greater than 5 then it this condition is true and the program institution comes here and sees break he would and then it comes out of the loop so let's see what happens now so let's run the script and let's see what happens so you can see it's printing this because I just need to give space here and once again let's run the script and you can see it trends 1 to 5 here right so whenever this condition is true this keyword is called and then the program goes out of the loop okay so it doesn't print up to 10 even if the you know execution of this for loop demands that we need to go up to 10 and then it goes out of the loop but we have given the special keyword break so whenever this condition is met then it goes out of the loop so this is how you can use break to come out of the loop or break the normal execution of the loop prematurely so before it's normal ending right now let's see how to use the continue statement now continue is a little bit different from break and what it does is whenever you use continue it skips the normal execution for that iteration so whatever comes after the continue keyword will be skipped and then the program will go to the next iteration right so let's say we have this condition I is equal to and let's say I is equal to 3 and or let's say we want to give the or operator here and let's say we say dollar i equals 6 okay so we are just saying here that if I is equal to 3 or I is equal to 6 then we will give the continue key word here okay and the continue keyword will skip the execution of the loop or whenever it sees three or five so let's see what happens so I'm going to just clear the screen and run the script once again and you can see it trends one to ten but when we see carefully here you see one two but there is no three here right and that is what we have written here and then you can see four five and there is no six here so whenever this condition is met where I is equal to three or I is equal to six then this condition is met and then this keyword continue is called and what it means is whenever your program or your script sees this keyword in the loop then whatever comes after this keyword will be skipped so for example this echo statement comes after this continue keyword then this will be skipped and your program will go to the next iteration so in case of three are discontinued continue keyword is called so this will be skipped that means three will not be printed and then in the condition of for this condition is not match so once again this will print using this echo and in the case of six this is called and the if this is called everything after the continue keyword will be skipped so it will not print six and it will go to the next iteration and then it sees that I is equal to seven and it will go normally until this condition is met okay so this is how break and continue keyword works in shell scripting I hope you have enjoyed this blog

Post a Comment

Previous Post Next Post

Recent in Technology News