{"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>\u00a0<\/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 \u201c(( matter++ ))\u201d:<\/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 \u201csleep 1\u201d 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 ] && [ $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 \u201cdone\u201d 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 \u201ccount number\u201d is much less than nine and \u201cfinished\u201d is identical to 0. Therefore the loops exit whilst the remember equals 5.<\/p>\n<p>The \u201c&&\u201d method logical \u201cand\u201d and \u201cway logical \u201cor\u201d.<\/p>\n<p>An opportunity notation for the conjunctions \u201cand\u201d and \u201cor\u201d in situations is \u201c-a\u201d and \u201c-o\u201d with single square brackets. The above circumstance:<\/p>\n<pre><code><strong>[[ $count -le 9 ] && [ $done == 0 ]]<\/strong><\/code><\/pre>\n<p>\u2026May 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 \u201cinventory.Txt:\u201d<\/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 \u201crecord\u201d variable. The second one line saves the \u201ctrendy enter\u201d within the document descriptor \u201c6\u201d (it can be any cost between 3 and 9). This is completed so that \u201cpopular input\u201d may be restored to report descriptor \u201czero\u201d at the give up of the script (see the statement \u201cexec 0 inside the 3rd line the input record is assigned to document descriptor \u201czero,\u201d which is used for wellknown enter. The \u201cexamine\u201d announcement then reads a line from the file on every new release and assigns it to the \u201cline1\u201d 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 \u201cecho completed.\u201d<\/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 \u201ccontinue\u201d announcement is achieved whilst the variable \u201cremember\u201d reaches five. This indicates the subsequent declaration (echo \u201c$count\u201d) isn\u2019t carried out on this generation (whilst the cost of \u201cdepend\u201d 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}]}}