{"id":32407,"date":"2022-12-23T06:03:44","date_gmt":"2022-12-23T06:03:44","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=32407"},"modified":"2022-12-23T06:12:04","modified_gmt":"2022-12-23T06:12:04","slug":"shell-scripting-tutorials-looping-iteration-statatement-using-while","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/shell-scripting-tutorials-looping-iteration-statatement-using-while\/","title":{"rendered":"Shell Scripting Tutorials: Looping &amp; Iteration statatement using while"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>The bash while loop is a control flow statement that allows code or commands to be executed repeatedly based on a given condition.<\/p>\n\n\n\n<p>Most languages have the concept of loops: If we want to repeat a task twenty times, we don&#8217;t want to have to type in the code twenty times, with maybe a slight change each time.<br>As a result, we have for and while loops in the Bourne shell.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Loops in Shell Scripting<\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"900\" height=\"500\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-1.jpg\" alt=\"\" class=\"wp-image-32419\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-1.jpg 900w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-1-300x167.jpg 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-1-768x427.jpg 768w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of Shell Scripting<\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"888\" height=\"259\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/image-19.png\" alt=\"\" class=\"wp-image-32421\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/image-19.png 888w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/image-19-300x88.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/image-19-768x224.png 768w\" sizes=\"auto, (max-width: 888px) 100vw, 888px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Control Flow of While Loop<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"715\" height=\"688\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-3.jpg\" alt=\"\" class=\"wp-image-32420\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-3.jpg 715w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-3-300x289.jpg 300w\" sizes=\"auto, (max-width: 715px) 100vw, 715px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Example of While Loop in Shell Script<\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"514\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-4-1024x514.jpg\" alt=\"\" class=\"wp-image-32422\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-4-1024x514.jpg 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-4-300x151.jpg 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-4-768x386.jpg 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-4.jpg 1131w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Boolean Operators When Working With Shell Scripts<\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Apart from the boolean operators that you\u2019ve seen in other programming languages, including&nbsp;<strong>==, &lt;, &gt;, &lt;=, &gt;=,<\/strong>&nbsp;etc., these are shell script specific operators. The major usage difference is that these operators will&nbsp;<strong>only work when numeric values<\/strong>&nbsp;are provided. With string value, the operation will simply fail.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Operator<\/th><th>Description<\/th><th>Example<\/th><\/tr><tr><td>-ne<\/td><td>Checks if both operands are not equal<\/td><td>[ 1 -ne 2 ] is TRUE<\/td><\/tr><tr><td><strong>\u2013<\/strong>eq<\/td><td>Checks if both the operands are equal<\/td><td>[ 1 -eq 2 ] is FALSE<\/td><\/tr><tr><td><strong>\u2013<\/strong>lt<\/td><td>Check if the left operand is lesser than the right operand<\/td><td>[1-lt 2 ] is TRUE<\/td><\/tr><tr><td><strong>\u2013<\/strong>gt<\/td><td>Check if the left operand is greater than the right operand<\/td><td>[1-gt 2 ] is FALSE<\/td><\/tr><tr><td><strong>\u2013<\/strong>le<\/td><td>Check if the left operand is less than or equal to the right.<\/td><td>[1-le 2 ] is TRUE<\/td><\/tr><tr><td><strong>\u2013<\/strong>ge<\/td><td>Check if the left operand is greater than or equal to the right.<\/td><td>[1-ge 2 ] is FALSE<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a While Loop in Shell Script<\/h2>\n\n\n\n<p>A while loop doesn\u2019t have a repetition limit by default. We have to explicitly provide it a condition that at some point during the code execution, will turn to false. Else you\u2019ll have a loop that doesn\u2019t end unless an interrupt signal is sent.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"625\" height=\"333\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/image-20.png\" alt=\"\" class=\"wp-image-32423\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/image-20.png 625w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/image-20-300x160.png 300w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/figure>\n\n\n\n<p>Save the script above by any name with the&nbsp;<strong>.sh<\/strong>&nbsp;extension. To run the file, you can either run it with the bash command like&nbsp;<strong>bash &lt;filename&gt;.sh<\/strong>&nbsp;or make the file executable using the chmod command. To make the script executable, run&nbsp;<strong>chmod +x &lt;filename&gt;.sh<\/strong>&nbsp;and then&nbsp;<strong>.\/&lt;filename&gt;.sh<\/strong>&nbsp;will allow you to run the script.<\/p>\n\n\n\n<p><strong>Let\u2019s understand the script line by line.<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>i=0<\/strong>&nbsp;\u2013 Here we set the variable $i to 0. Learn more about&nbsp;<a href=\"https:\/\/www.linuxfordevices.com\/tutorials\/shell-script\/variables-in-shell-scripts\" target=\"_blank\" rel=\"noopener\">variables<\/a>&nbsp;in our previous tutorial<\/li>\n\n\n\n<li><strong>while [ $i -le 10 ]<\/strong>&nbsp;\u2013 Run the while loop only until the variable $i is lesser than or equal to 10. So this loop will run 11 times including the&nbsp;<em>ZERO-th<\/em>&nbsp;run.<\/li>\n\n\n\n<li><strong>do<\/strong>&nbsp;\u2013 Marks the beginning of the while loop in shell scripts<\/li>\n\n\n\n<li><strong>echo True<\/strong>&nbsp;\u2013 A simple command that will print the word \u201cTrue\u201d on our terminal<\/li>\n\n\n\n<li><strong>((i++))<\/strong>&nbsp;\u2013 C style variable increments to&nbsp;<strong>add 1<\/strong>&nbsp;to the variable i with every loop. There are various other ways to perform an increment include $i=((i+1)), ((i=i+1)), etc. Feel free to use either one.<\/li>\n\n\n\n<li><strong>done<\/strong>&nbsp;\u2013 Marks the end of the while loop<\/li>\n\n\n\n<li><strong>echo False<\/strong>&nbsp;\u2013 This again simply prints out the word False on the screen to indicate that condition has now turned false.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Example of nested While Loop<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"825\" height=\"541\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-5.jpg\" alt=\"\" class=\"wp-image-32424\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-5.jpg 825w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-5-300x197.jpg 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-5-768x504.jpg 768w\" sizes=\"auto, (max-width: 825px) 100vw, 825px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/66e761a48725c9763f1d8e92bccefc55.js\"><\/script>\n","protected":false},"excerpt":{"rendered":"<p>The bash while loop is a control flow statement that allows code or commands to be executed repeatedly based on a given condition. Most languages have the concept of loops: If we want to repeat a task twenty times, we don&#8217;t want to have to type in the code twenty times, with maybe a slight&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-32407","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32407","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=32407"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32407\/revisions"}],"predecessor-version":[{"id":32426,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32407\/revisions\/32426"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=32407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=32407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=32407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}