{"id":30524,"date":"2018-03-01T17:51:20","date_gmt":"2018-03-01T12:21:20","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=30524"},"modified":"2018-03-01T17:51:20","modified_gmt":"2018-03-01T12:21:20","slug":"write-bash-loops","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/write-bash-loops\/","title":{"rendered":"How to Write Bash WHILE-Loops"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>You may execute a sequence of commands through writing them into a script file and then running it.<\/p>\n<p>A script file is really a textual content file, generally with the .Sh document extension, that contains a sequence of instructions that would additionally be finished from the command line (shell).<\/p>\n<h4 id=\"at-the-same-time-as-loop-examples\"><strong>At the same time as loop examples<\/strong><\/h4>\n<p>Under is an instance of a while loop. Whilst performed, this script file will print the numbers 1 via 9 at the screen.<\/p>\n<p>The whilst-assertion offers you greater flexibility for specifying the termination situation than the for-loop.<\/p>\n<pre><code><strong>#!\/bin\/bash\r\ncount=1\r\nwhile [ $count -le 9 ]\r\ndo\r\necho \"$count\"\r\nsleep 1\r\n(( count++ ))\r\ndone<\/strong><\/code><\/pre>\n<p>For example, you can make the preceding script an limitless loop with the aid of omitting the increment statement &#8220;(( matter++ ))&#8221;:<\/p>\n<pre><code><strong>#!\/bin\/bash\r\ncount=1\r\nwhile [ $count -le 9 ]\r\ndo\r\necho \"$count\"\r\nsleep 1\r\ndone<\/strong><\/code><\/pre>\n<p>The &#8220;sleep 1&#8221; announcement pauses the execution for 1 second on each generation. Use the ctrl+c keyboard shortcut to terminate the procedure.<\/p>\n<p>You may also create an countless loop by way of placing a colon because the circumstance:<\/p>\n<pre><code><strong>#!\/bin\/bash\r\ncount=1\r\nwhile :\r\ndo\r\necho \"$count\"\r\nsleep 1\r\n(( count++ ))\r\ndone<\/strong><\/code><\/pre>\n<p>So as to use a couple of situations within the while-loop, you need to use the double rectangular bracket notation:<\/p>\n<pre><code><strong>count=1\r\ndone=0\r\nwhile [[ $count -le 9 ] &amp;&amp; [ $done == 0 ]]\r\ndo\r\necho \"$count\"\r\nsleep 1\r\n(( count++ ))\r\nif [ $count == 5 ]; then $done=1\r\nfi\r\ndone<\/strong><\/code><\/pre>\n<p>In this script, the variable &#8220;done&#8221; is initialized to 0 after which set to 1 whilst the be counted reaches 5. The loop condition states that the even as loop will maintain so long as &#8220;count number&#8221; is much less than nine and &#8220;finished&#8221; is identical to 0. Therefore the loops exit whilst the remember equals 5.<\/p>\n<p>The &#8220;&amp;&amp;&#8221; method logical &#8220;and&#8221; and &#8220;way logical &#8220;or&#8221;.<\/p>\n<p>An opportunity notation for the conjunctions &#8220;and&#8221; and &#8220;or&#8221; in situations is &#8220;-a&#8221; and &#8220;-o&#8221; with single square brackets. The above circumstance:<\/p>\n<pre><code><strong>[[ $count -le 9 ] &amp;&amp; [ $done == 0 ]]<\/strong><\/code><\/pre>\n<p>&#8230;May be rewritten as:<\/p>\n<pre><code><strong>[ $count -le 9 ] -a [ $done == 0 ]<\/strong><\/code><\/pre>\n<p>Analyzing a text report is commonly completed with some time loop. Inside the following instance, the bash script reads the contents line by line of a document referred to as &#8220;inventory.Txt:&#8221;<\/p>\n<pre><code><strong>FILE=inventory.txt\r\nexec 6<\/strong><\/code><\/pre>\n<p>The first line assigns the enter report name to the &#8220;record&#8221; variable. The second one line saves the &#8220;trendy enter&#8221; within the document descriptor &#8220;6&#8221; (it can be any cost between 3 and 9). This is completed so that &#8220;popular input&#8221; may be restored to report descriptor &#8220;zero&#8221; at the give up of the script (see the statement &#8220;exec 0 inside the 3rd line the input record is assigned to document descriptor &#8220;zero,&#8221; which is used for wellknown enter. The &#8220;examine&#8221; announcement then reads a line from the file on every new release and assigns it to the &#8220;line1&#8221; variable.<\/p>\n<p>To be able to prematurely go out some time-loop, you could use the ruin assertion like this:<\/p>\n<pre><strong><code>count=1\r\ndone=0\r\nwhile [ $count -le 9 ]\r\ndo\r\necho \"$count\"\r\nsleep 1\r\n(( count++ ))\r\nif [ $count == 5 ]\r\nthen\r\nbreak\r\nfi\r\ndone\r\necho Finished<\/code><\/strong><\/pre>\n<p>The break assertion skips program execution to the stop whilst loop and executes any statements following it.<\/p>\n<p>In this example, the announcement &#8220;echo completed.&#8221;<\/p>\n<p>The maintain declaration, alternatively, skips only the relaxation of the while loop announcement of the contemporary iteration and jumps at once to the following generation:<\/p>\n<pre><strong><code>count=1\r\ndone=0\r\nwhile [ $count -le 9 ]\r\ndo\r\nsleep 1\r\n(( count++ ))\r\nif [ $count == 5 ]\r\nthen\r\ncontinue\r\nfi\r\necho \"$count\"\r\ndone\r\necho Finished<\/code><\/strong><\/pre>\n<p>In this situation, the &#8220;continue&#8221; announcement is achieved whilst the variable &#8220;remember&#8221; reaches five. This indicates the subsequent declaration (echo &#8220;$count&#8221;) isn&#8217;t carried out on this generation (whilst the cost of &#8220;depend&#8221; is 5).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A script file is really a textual content file, generally with the .Sh document extension, that contains a sequence of instructions that would additionally<\/p>\n","protected":false},"author":2,"featured_media":30534,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1699],"tags":[84525,84526,84527,84524,84528,84523,84530,84529],"class_list":["post-30524","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","tag-bash-do-while-loop","tag-bash-for-loop","tag-bash-for-loop-one-line","tag-bash-infinite-while-loop","tag-bash-while-not","tag-bash-while-true-loop","tag-do-while-loop-in-shell-script","tag-while-loop-in-shell-script-to-read-file"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/30524","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=30524"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/30524\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/30534"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=30524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=30524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=30524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}