{"id":2288,"date":"2017-12-08T06:29:27","date_gmt":"2017-12-08T06:29:27","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/?p=2288"},"modified":"2020-01-09T09:55:35","modified_gmt":"2020-01-09T09:55:35","slug":"nested-for-loop-in-linux-shell-script","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/nested-for-loop-in-linux-shell-script\/","title":{"rendered":"Nested for loop in Linux Shell Script"},"content":{"rendered":"<p><strong>scmuser created the topic: Nested for loop in Linux Shell Script<\/strong><\/p>\n<p>As you see the if statement can nested, similarly loop statement can be nested. You can nest the for loop. To understand the nesting of for loop see the following shell script.<br \/>\n$ vi nestedfor.sh<br \/>\nfor (( i = 1; i <= 5; i++ )) ### Outer for loop ###\ndo\n\nfor (( j = 1 ; j <= 5; j++ )) ### Inner for loop ###\ndo\necho -n \"$i \"\ndone\n\necho \"\" #### print the new line ###\n\ndone\n\nRun the above script as follows:\n$ chmod +x nestedfor.sh\n$ .\/nestefor.sh\n1 1 1 1 1\n2 2 2 2 2\n3 3 3 3 3\n4 4 4 4 4\n5 5 5 5 5\n\nHere, for each value of i the inner loop is cycled through 5 times, with the varible j taking values from 1 to 5. The inner for loop terminates when the value of j exceeds 5, and the outer loop terminets when the value of i exceeds 5.\n\nFollowing script is quite intresting, it prints the chess board on screen.\n\n\n$ vi chessboard\nfor (( i = 1; i <= 9; i++ )) ### Outer for loop ###\ndo\nfor (( j = 1 ; j <= 9; j++ )) ### Inner for loop ###\ndo\ntot=`expr $i + $j`\ntmp=`expr $tot % 2`\nif [ $tmp -eq 0 ]; then\necho -e -n \"\\033[47m \"\nelse\necho -e -n \"\\033[40m \"\nfi\ndone\necho -e -n \"\\033[40m\" #### set back background colour to black\necho \"\" #### print the new line ###\ndone\n\nRun the above script as follows:\n$ chmod +x chessboard\n$ .\/chessboard\n\nAbove shell script cab be explained as follows:\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>scmuser created the topic: Nested for loop in Linux Shell Script As you see the if statement can nested, similarly loop statement can be nested. You can nest the for&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[454],"tags":[460],"class_list":["post-2288","post","type-post","status-publish","format-standard","hentry","category-shell-script","tag-linux"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2288","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=2288"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2288\/revisions"}],"predecessor-version":[{"id":2289,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2288\/revisions\/2289"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=2288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=2288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=2288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}