{"id":417,"date":"2014-12-05T17:52:21","date_gmt":"2014-12-05T15:52:21","guid":{"rendered":"http:\/\/www.bashpi.org\/?page_id=417"},"modified":"2020-12-31T17:58:47","modified_gmt":"2020-12-31T15:58:47","slug":"while","status":"publish","type":"page","link":"https:\/\/www.bashpi.org\/?page_id=417","title":{"rendered":"while &#038; until cycle in bash"},"content":{"rendered":"<h4>While cycle runs until the condition is true<\/h4>\n<p><strong>This while cycle generates random numbers<\/strong> in range from 0 to 1000 and exits if number is greater than 900.<\/p>\n<pre><code class=\"language-bash\">while true ; do x=$[ ( $RANDOM % 1000 ) ]; echo $x; if [ $x -gt 900 ]; then break; fi;done\n<\/code><\/pre>\n<p><strong>Runs in background and runs something.sh as soon as there is no more .lock files in the current directory.<\/strong> 1 second sleep between checks.<\/p>\n<pre><code class=\"language-bash\">while ls *.lock &amp;&gt; \/dev\/null ; do sleep 1;done &amp;&amp; something.sh &amp;\n<\/code><\/pre>\n<p><strong>Keeps something.sh running<\/strong>. As soon as something.sh exits because of some reason, it runs alert.sh and starts something.sh again. Quite easy way to restart some program when it exits and get alerts if it stops. Its note quite the thing with nohup but still&#8230;<\/p>\n<pre><code class=\"language-bash\">while true; do something.sh; alert.sh;done\n<\/code><\/pre>\n<p><strong>Random service interrupts<\/strong> &#8211; run service with downtimes up to 1800sec and uptimes up to 3600sec<\/p>\n<pre><code class=\"language-bash\">while true ; do X=$[ ( $RANDOM % 1800 ) ]; echo \"Down for ${X} seconds\"; service httpd stop; sleep ${X}; Y=$[ ( $RANDOM % 3600 ) ]; echo \"Up for ${Y} seconds\"; service httpd start; sleep ${Y}; done\n<\/code><\/pre>\n<p><strong>Add command line arguments to array and append to string<\/strong><\/p>\n<pre>declare -A argumentarray\nargumentstring=\"appending arguments to this string separated by comma: \"\n\nwhile [ $# -gt 0 ];do\n  argumentarray[${1}]=\"${1}\"\n  argumentstring+=\"${1}\"\n  [ $# -ge 2 ] &amp;&amp; argumentstring+=\",\"\n  shift\ndone<\/pre>\n<p><strong>Loop trough each line of the file with while<\/strong><\/p>\n<pre># cycle trough each line in file\nwhile read LINE; do \necho \"${LINE}\"\ndone &lt; inputfile\n\n# OR pipe input to it\ncat inputfile | while read LINE; do \necho \"${LINE}\"\ndone<\/pre>\n<h4><\/h4>\n<h4>Until cycle runs until the condition is FALSE<\/h4>\n<p>If you want to run cycle until condition is false, replace <em>while<\/em> with <strong><em>until<\/em><\/strong>, it&#8217;s identical to <em>while<\/em> but with negated condition.\u00a0 As an example, following would be infinite loop with <em>until<\/em>:<\/p>\n<pre><code class=\"language-bash\">until false ; do echo \"Echoing to infinity\"; done\n<\/code><\/pre>\n<p>Check out also <a title=\"for\" href=\"http:\/\/www.bashpi.org\/?page_id=411\">for cycle<\/a><\/p>\n<p>If you found this useful, say thanks, click on some banners or <a href=\"http:\/\/www.bashpi.org\/?page_id=105\">donate<\/a>, I can always use some beer money.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While cycle runs until the condition is true This while cycle generates random numbers in range from 0 to 1000 and exits if number is greater than 900. while true ; do x=$[ ( $RANDOM % 1000 ) ]; echo $x; if [ $x -gt 900 ]; then break; fi;done Runs in background and runs [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":397,"menu_order":0,"comment_status":"closed","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-417","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/pages\/417","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bashpi.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=417"}],"version-history":[{"count":12,"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/pages\/417\/revisions"}],"predecessor-version":[{"id":1098,"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/pages\/417\/revisions\/1098"}],"up":[{"embeddable":true,"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/pages\/397"}],"wp:attachment":[{"href":"https:\/\/www.bashpi.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}