{"id":193,"date":"2020-08-18T19:23:47","date_gmt":"2020-08-18T20:23:47","guid":{"rendered":"http:\/\/www.linux-tutorial.info\/?page_id=77"},"modified":"2020-08-22T19:25:16","modified_gmt":"2020-08-22T20:25:16","slug":"this-is-the-page-title-toplevel-27","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=193","title":{"rendered":"A Few More Constructs"},"content":{"rendered":"\n<title>A Few More Constructs<\/title>\n<p>\nThere are a few more loop constructs that we ought to cover as you are\nlikely to come across them in some of the system scripts. The first is for a\nfor-loop and has the following syntax:<\/p>\n<p>\n<commandexample command=\"bash\">\nfor var in word1 word2 &#8230;\ndo\nlist of commands\ndone\n<\/commandexample>\n<p>\nWe might use this to list a set of pre-defined directories like this:<\/p>\n<p>\n<commandexample command=\"bash\">\nor dir in bin etc usr\ndo\nls -R $dir\ndone\n<\/commandexample>\n<p>\nThis script does a recursive listing three times. The first time through the\nloop, the <glossary>variable<\/glossary>\ndir is assigned the value bin, next etc, and finally usr.\n<\/p>\n<p>\nYou may also see that the do\/done pair can be replaced by curly braces ({\n}). So, the script above would look like this:<\/p>\n<p>\n<commandexample command=\"bash\">\nfor dir in bin etc usr\n{\nls -R $dir\n}\n<\/commandexample>\n<\/p>\n<p>\nNext, we have while loops. This construct is used to repeat a loop while a\ngiven expression is true. Although you can use it by itself, as in\n<\/p>\n<p>\n<commandexample command=\"bash\">while ( $VARIABLE=value)<\/commandexample>\n<\/p>\n<p>\nI almost exclusively use it at the end of a <glossary>pipe<\/glossary>.\n For example:<\/p>\n<p>\n<commandexample command=\"bash\">\ncat filename | while read line\ndo\ncommands\ndone\n<\/commandexample>\n<p>\nThis sends the contents of the file <i>filename<\/i> through the\n<glossary>pipe<\/glossary>, which reads\none line at a time. Each line is assigned to <glossary>variable<\/glossary>\nline. I can then process\neach line, one at a time. This is also the format that many of the system\nscripts use.\n<\/p>\n<p>\nFor those of you who have worked with <glossary>UNIX<\/glossary>\nshells before, you most certainly\nshould have noticed that I have left out some constructs. Rather than turning\nthis into a book on <glossary>shell<\/glossary>\nprogramming, I decided to show you the constructs that\noccur most often in the <glossary>shell<\/glossary>\nscripts on your system. I will get to others as we\nmove along. The man-pages of each of the shells provide more details.\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A Few More Constructs There are a few more loop constructs that we ought to cover as you are likely to come across them in some of the system scripts. The first is for a for-loop and has the following &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=193\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-193","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=193"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/193\/revisions"}],"predecessor-version":[{"id":488,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/193\/revisions\/488"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}