{"id":411,"date":"2014-12-02T21:34:49","date_gmt":"2014-12-02T19:34:49","guid":{"rendered":"http:\/\/www.bashpi.org\/?page_id=411"},"modified":"2021-02-04T18:47:18","modified_gmt":"2021-02-04T16:47:18","slug":"for","status":"publish","type":"page","link":"https:\/\/www.bashpi.org\/?page_id=411","title":{"rendered":"for cycle \/ loop"},"content":{"rendered":"<h4>For cycle runs once for every argument<\/h4>\n<p>Run something.sh script 100 times with parameters from 1 to 100<\/p>\n<pre><code class=\"language-bash\">for i in `seq 100`;do something.sh $i;done\n<\/code><\/pre>\n<p><strong>Another way of doing something with found files\/directories.<\/strong> Not so effective than using <em>-exec<\/em> in <em>find<\/em> command but it will do just fine with small amount of results. Searches for directories which have 750 permissions and makes them group-executable.<\/p>\n<pre><code class=\"language-bash\">for f in `find . -type d -perm 750`;do chmod o+x $f;done\n<\/code><\/pre>\n<p><strong>Do something with every entry in file.<\/strong> This example searches for users listed in cron.allow from \/etc\/shadow. Good way to determine do all these users have password set.<\/p>\n<pre><code class=\"language-bash\">for f in `cat \/etc\/cron.d\/cron.allow`;do grep $f \/etc\/shadow ;done<\/code><\/pre>\n<p><strong>Rename files, remove certain string from filename.<\/strong> Current example removes all strings matching &#8220;sh?t&#8221; like shit, shut, shot etc. from all *.zip files in current directory.\u00a0 Also generates harmless mv command errors if *.zip filename does not contain any of those strings.<\/p>\n<pre><code class=\"language-bash\">for f in `ls *.zip`;do mv $f `echo $f | sed -e s\/sh.t\/\/g`;done\n<\/code><\/pre>\n<p><strong>Find users who are removed but homedirs are kept.<\/strong> Also finds all users who have external\u00a0 authentication provider.<\/p>\n<pre><code class=\"language-bash\">for f in `ls -la \/home\/ | awk '{print $3;}'`;do if ( ! grep $f \/etc\/passwd &gt; \/dev\/null ); then echo $f; fi; done\n<\/code><\/pre>\n<p><strong>Find files with acl-s<\/strong> from subdirectories dir1, dir2, dir3<\/p>\n<pre><code class=\"language-bash\">for f in dir1 dir2 dir3 ;do find .\/$f -acl ;done\n<\/code><\/pre>\n<p><strong>Check open file count for all http processes<\/strong>, if any of them has over 1024 files open, restart httpd service<\/p>\n<pre><code class=\"language-bash\">for f in `ps -ef |grep \"\/usr\/sbin\/httpd\" |grep -v \"grep\" |awk '{print $2}'`;do if [ `lsof -p ${f} |wc -l` -gt 1024 ]; then service httpd restart ; break; fi ;done\n<\/code><\/pre>\n<p><strong>Unzip all .gz files<\/strong> found from \/foo to \/bar<\/p>\n<pre><code class=\"language-bash\">for f in `find \/foo -name \"*.gz\"`; do zcat ${f} &gt; \/bar\/`basename ${f}|sed -e 's\/\\.gz$\/\/'` ;done\n<\/code><\/pre>\n<p><strong>for cycle trough an array<\/strong><\/p>\n<pre><code class=\"language-bash\"># declare array\ndeclare -A myarray\n# add few keys and values into array\nmyarray[\"first\"]=\"foo\"\nmyarray[\"second\"]=\"bar\"\nmyarray[0]=1\n\nfor KEY in \"${!myarray[@]}\"; do\nVAL=\"${myarray[${KEY}]}\"\necho \"${KEY}:${VAL}\"\ndone\n<\/code><\/pre>\n<p>&#8230;.<\/p>\n<p>Check out also <a title=\"while\" href=\"http:\/\/www.bashpi.org\/?page_id=417\">while 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>For cycle runs once for every argument Run something.sh script 100 times with parameters from 1 to 100 for i in `seq 100`;do something.sh $i;done Another way of doing something with found files\/directories. Not so effective than using -exec in find command but it will do just fine with small amount of results. Searches for [&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-411","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/pages\/411","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=411"}],"version-history":[{"count":17,"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/pages\/411\/revisions"}],"predecessor-version":[{"id":1204,"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/pages\/411\/revisions\/1204"}],"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=411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}