{"id":41,"date":"2022-03-04T15:11:17","date_gmt":"2022-03-04T20:11:17","guid":{"rendered":"https:\/\/pressbooks.library.ryerson.ca\/opsyshiraki\/chapter\/controlstruct2\/"},"modified":"2025-02-03T19:35:02","modified_gmt":"2025-02-04T00:35:02","slug":"controlstruct2","status":"publish","type":"chapter","link":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/chapter\/controlstruct2\/","title":{"raw":"Control Structures - Part 2 - Loops","rendered":"Control Structures &#8211; Part 2 &#8211; Loops"},"content":{"raw":"<p class=\"c13 c21\"><span class=\"c3\">Eventually you will need to repeat actions. \u00a0A practical way to repeat actions without repeating code is achieved with the construct of a loop. <\/span><\/p>\r\n\r\n<div class=\"textbox shaded\">\r\n<h1 id=\"h.32v1nhr0jhri\" class=\"c21 c4\"><span class=\"c25\">Computer Science Loop Concepts<\/span><\/h1>\r\n<p class=\"c6\"><span class=\"c3\">The construct of a loop allows for the repetition of actions without repeating code. \u00a0<\/span><\/p>\r\n\r\n<h2 class=\"c5\"><span class=\"c11\">General Loop (Concept)<\/span><\/h2>\r\n<p class=\"c6\"><span class=\"c3\">The most general flow of control is illustrated in the flowchart below (General). \u00a0Observe that there can be a set of actions (Block 1) which is performed regardless on entry into the loop. \u00a0Then a conditional check is made to determine continuation of the loop. \u00a0If satisfied, further actions within the loop (Block 2) will be performed. \u00a0<\/span><\/p>\r\n<p class=\"c5\"><span class=\"c3\">\u00a0<\/span><\/p>\r\n<p class=\"c6\">Not all computer languages support the General loop construct (Unix\r\n<span class=\"c29\">does <\/span><span class=\"c3\">support a General loop). \u00a0If a computer language does not support the General loop construct, it will usually support one or more of the specific cases of the General loop. \u00a0For example, C Language supports both a While and a Do-While construct but not the General loop construct.<\/span><\/p>\r\n\r\n<h2 id=\"h.s7we9xpua7pz\" class=\"c4 c7\"><span class=\"c11\">While Loop (Concept)<\/span><\/h2>\r\n<p class=\"c6\"><span class=\"c3\">The While Loop, as a concept, is a special case of a General Loop which has no actions for Block 1. \u00a0In this case, the condition is checked as a first action on entry into the loop construct. \u00a0See flowchart below (While).<\/span><\/p>\r\n\r\n<h2 id=\"h.ds9d1tv3r7e2\" class=\"c7 c4\"><span class=\"c11\">Do-While Loop (Concept)<\/span><\/h2>\r\n<p class=\"c6\"><span class=\"c3\">The Do-While Loop, as a concept, is a special case of a General Loop which has no actions for Block 2. \u00a0In this case, the Block 1 code is executed on entry into the loop construct, and the condition is checked at the end of the loop construct. \u00a0See flowchart below (Do-While).<\/span><\/p>\r\n\r\n<table class=\"grid\">\r\n<thead>\r\n<tr class=\"c18\">\r\n<th class=\"c8\" colspan=\"1\" rowspan=\"1\" scope=\"col\">\r\n<p class=\"c2\"><span class=\"c3\">General<\/span><\/p>\r\n<\/th>\r\n<th class=\"c8\" colspan=\"1\" rowspan=\"1\" scope=\"col\">\r\n<p class=\"c2\"><span class=\"c3\">While<\/span><\/p>\r\n<\/th>\r\n<th class=\"c8\" colspan=\"1\" rowspan=\"1\" scope=\"col\">\r\n<p class=\"c2\"><span class=\"c3\">Do-While<\/span><\/p>\r\n<\/th>\r\n<\/tr>\r\n<\/thead>\r\n<tbody>\r\n<tr class=\"c32\">\r\n<td class=\"c8\" colspan=\"1\" rowspan=\"1\">\r\n<p class=\"c2\"><span style=\"overflow: hidden;margin: 0.00px 0.00px;border: 0.00px solid #000000;width: 178.50px;height: 264.67px\">\r\n<img alt=\"General loop has arrow to block 1 before decision and block 2 after decision; decision has 2 arrows out with true path leading to block 2 and false path leaving loop; arrow from block 2 loops back to block 1.\" src=\"http:\/\/pressbooks.library.ryerson.ca\/opsyshiraki\/wp-content\/uploads\/sites\/330\/2022\/03\/image3-1.png\" style=\"width: 178.50px;height: 264.67px;margin-left: 0.00px;margin-top: 0.00px\" width=\"178\" height=\"257\" class=\"alignnone\" \/>\r\n<\/span><\/p>\r\n<\/td>\r\n<td class=\"c8\" colspan=\"1\" rowspan=\"1\">\r\n<p class=\"c2\"><span style=\"overflow: hidden;margin: 0.00px 0.00px;border: 0.00px solid #000000;width: 181.50px;height: 269.12px\">\r\n<img alt=\"While has arrow to decision (no block 1) with arrow to block 2 then arrow from block 2 loops back to decision.\" src=\"http:\/\/pressbooks.library.ryerson.ca\/opsyshiraki\/wp-content\/uploads\/sites\/330\/2022\/03\/image2-1.png\" style=\"width: 181.50px;height: 269.12px;margin-left: 0.00px;margin-top: 0.00px\" width=\"181\" height=\"262\" class=\"alignnone\" \/>\r\n<\/span><\/p>\r\n<\/td>\r\n<td class=\"c8\" colspan=\"1\" rowspan=\"1\">\r\n<p class=\"c2\"><span style=\"overflow: hidden;margin: 0.00px 0.00px;border: 0.00px solid #000000;width: 190.99px;height: 276.44px\">\r\n<img alt=\"Do-while has has arrow to block 1 before decision (no block2). True output of decision box loops back to block 1.\" src=\"http:\/\/pressbooks.library.ryerson.ca\/opsyshiraki\/wp-content\/uploads\/sites\/330\/2022\/03\/image1-2.png\" style=\"width: 190.99px;height: 276.44px;margin-left: 0.00px;margin-top: 0.00px\" width=\"190\" height=\"268\" class=\"alignnone\" \/>\r\n<\/span><\/p>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<p class=\"c21 c13 c9\"><span class=\"c3\">\u00a0<\/span><\/p>\r\n\r\n<\/div>\r\n<span>[h5p id=\"3\"]<\/span>\r\n\r\n<span>[h5p id=\"4\"]<\/span>\r\n<h1 id=\"h.v5u5941k43y1\" class=\"c21 c4\"><span class=\"c30\">Loops in Unix<\/span><\/h1>\r\n<p class=\"c6\">There are three commands in Unix to support loops.<\/p>\r\n\r\n<ul class=\"c14 lst-kix_r52uojy9d8wy-0 start\">\r\n \t<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">while<\/span><\/li>\r\n \t<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">until\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/li>\r\n \t<li class=\"c6 c15 li-bullet-0\">for<\/li>\r\n<\/ul>\r\n<h2 id=\"h.1g17zi2rpgp\" class=\"c21 c4\"><span class=\"c25\">while command<\/span><\/h2>\r\n<p class=\"c12\"><span class=\"c3\">The while command in Unix implements the General Loop construct discussed earlier.<\/span><\/p>\r\n&nbsp;\r\n<table class=\"grid\">\r\n<thead>\r\n<tr class=\"c18\">\r\n<th class=\"c17\" scope=\"col\">\r\n<p class=\"c12\"><span class=\"c3\">general syntax<\/span><\/p>\r\n<\/th>\r\n<th class=\"c17\" scope=\"col\">\r\n<p class=\"c2\"><span class=\"c11\">relation to flow chart<\/span><\/p>\r\n<\/th>\r\n<\/tr>\r\n<\/thead>\r\n<tbody>\r\n<tr>\r\n<td>\r\n<pre class=\"c12\" style=\"margin-top: 1em\"><span class=\"c0\">while list1<\/span>\r\n<span class=\"c0\">do<\/span>\r\n<span class=\"c0\">  list2<\/span>\r\n<span class=\"c26\">done\r\n<\/span><\/pre>\r\n<\/td>\r\n<td>\r\n<pre id=\"h.4nau2ymu2e5j\" class=\"c2 c20\" style=\"margin-top: 1em\"><span class=\"c0\">while block1<\/span>\r\n<span class=\"c0\">  \u00a0 \u00a0 check<\/span>\r\n<span class=\"c0\">do<\/span>\r\n<span class=\"c0\">  \u00a0block 2<\/span>\r\n<span class=\"c0\">done\r\n<\/span><\/pre>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<p class=\"c12\">Unix runs the <span style=\"text-decoration: underline\"><span class=\"c28\">list1 <\/span><\/span>and looks at the return code ($?) of the <span class=\"c35\">last command <\/span>in <span class=\"c28\">list.<\/span>\u00a0 (If there's only one command in the <span style=\"text-decoration: underline\"><span class=\"c28\">list1<\/span><\/span>, then that's the last command.) \u00a0If the return code of this last command is TRUE, then <span class=\"c28\"><span style=\"text-decoration: underline\">list2<\/span> <\/span>(loop body) is run. \u00a0Once the keyword \"done\" is reached, control returns to re-run <span class=\"c28\"><span style=\"text-decoration: underline\">list1<\/span> <\/span>and the return code of the last command in <span class=\"c28\"><span style=\"text-decoration: underline\">list1<\/span> <\/span><span class=\"c3\">is checked. \u00a0 If the return code of this last command is FALSE, then the while loop terminates, and control continues after the keyword \"done\".<\/span><\/p>\r\n&nbsp;\r\n<div class=\"textbox textbox--examples\"><header class=\"textbox__header\">\r\n<p class=\"textbox__title\">E.g. 1: Count from 1 to 12<\/p>\r\n\r\n<\/header>\r\n<div class=\"textbox__content\">\r\n<pre class=\"c2 c13\"><span class=\"c0\">month=1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span>\r\n<span class=\"c0\">while echo Checking limit against month ${month}<\/span>\r\n<span class=\"c26\">  [ ${month} -le 12 ]\u00a0\u00a0\u00a0# The test is the last command in list1<\/span>\r\n<span class=\"c0\">do<\/span>\r\n<span class=\"c26\">  \u00a0echo Performing action for month ${month}<\/span>\r\n<span class=\"c26\">  \u00a0month=$(( ${month} + 1))<\/span>\r\n<span class=\"c26\">done<\/span><span class=\"c3\">\u00a0\r\necho Value of month outside loop is ${month}<\/span><\/pre>\r\n<h3 class=\"c33 c4\" id=\"h.mjqovltwcnos\">Notes and observations<\/h3>\r\n<ol class=\"c14 lst-kix_oax79x4ju1qf-0 start\" start=\"1\">\r\n \t<li class=\"c12 c15 li-bullet-0\"><span class=\"c3\">The last command of list1 is a test command using the left bracket synonym.<\/span><\/li>\r\n \t<li class=\"c12 c15 li-bullet-0\"><span class=\"c3\">The -le is a test command option for the numeric comparison less than or equal to.<\/span><\/li>\r\n \t<li class=\"c12 c15 li-bullet-0\"><span class=\"c3\">As Unix program variables hold strings, to perform arithmetic operations, one must use the $(( .. )) syntax with dollar sign and double parentheses.<\/span><\/li>\r\n \t<li class=\"c12 c15 li-bullet-0\"><span class=\"c3\">The formal spelling of Unix variables requires enclosure in set braces { }. \u00a0Where there is no ambiguity, it is common practice in Unix to omit the set braces.<\/span><\/li>\r\n \t<li>Try running this code and see that the value of month never exceeds 12 in the line \"echo Performing action ...\".<\/li>\r\n<\/ol>\r\n<\/div>\r\n<\/div>\r\n&nbsp;\r\n<h3 class=\"c7 c4\" id=\"h.iodnffrfpn6n\"><span class=\"c11\">Preamble: Redirecting input from a file<\/span><\/h3>\r\n<pre id=\"h.db51xr7z9zar\" class=\"c2 c4 \"><span class=\"c0\">$ <strong>cat novel<\/strong><\/span>\r\n<span class=\"c0\">It was a<\/span>\r\n<span class=\"c0\">dark and<\/span>\r\n<span class=\"c0\">stormy<\/span>\r\n<span class=\"c0\">$ <strong>read oneline &lt; novel<\/strong><\/span>\r\n<span class=\"c0\">$ <strong>echo $oneline<\/strong><\/span>\r\n<span class=\"c0\">It was a<\/span>\r\n<span class=\"c0\">$ <strong>read oneline &lt; novel<\/strong><\/span>\r\n<span class=\"c0\">$ <strong>echo $oneline<\/strong><\/span>\r\n<span class=\"c0\">It was a<\/span>\r\n<span class=\"c0\">$<\/span><\/pre>\r\n<p class=\"c5\"><span class=\"c3\">\u00a0<\/span><\/p>\r\n<p class=\"c6\">Be reminded that redirection operators in Unix (&lt;, &gt;, etc.) apply only to the current command. \u00a0Once the command is over, redirection is restored to the default configuration (STDIN from keyboard, STDOUT to screen, etc.). \u00a0 Thus, observe that each time the read command is run, redirection is applied anew, and the first line of the file is read in.<\/p>\r\n\r\n<div class=\"textbox textbox--examples\"><header class=\"textbox__header\">\r\n<p class=\"textbox__title\">Eg 2a: How to read every line of a file (a non-functional example)<\/p>\r\n\r\n<\/header>\r\n<div class=\"textbox__content\" style=\"text-align: left\">\r\n<p class=\"c12\">The objective is to read a file one line at a time. \u00a0Here is a newbie's first attempt.<\/p>\r\n<p class=\"c12 c9\"><span class=\"c3\">\u00a0<\/span><\/p>\r\n\r\n<pre id=\"h.cuirxscuuipk\" class=\"c2 c4 \"><span class=\"c0\">$ <strong>cat badwhile<\/strong><\/span>\r\n<span class=\"c0\">while read wholeline &lt; novel<\/span>\r\n<span class=\"c0\">do<\/span>\r\n<span class=\"c0\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0something $wholeline \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#e.g. echo $wholeline<\/span>\r\n<span class=\"c0\">done<\/span>\r\n<span class=\"c0\">$ <strong>.\/badwhile<\/strong><\/span>\r\n<span class=\"c0\">It was a<\/span>\r\n<span class=\"c0\">It was a<\/span>\r\n<span class=\"c0\">It was a<\/span>\r\n<span class=\"c0\">It was a<\/span>\r\n...\r\n\r\n<span class=\"c3\">\r\n<\/span><span class=\"c3\"><\/span><\/pre>\r\nWhat happened? \u00a0The redirection operator applies to the read command. \u00a0Each time the read command is run, redirection is applied anew and only the first line of the file is ever read.\r\n\r\n&nbsp;\r\n\r\n<\/div>\r\n<\/div>\r\n&nbsp;\r\n\r\nHow can one fix this problem?\r\n<div class=\"textbox textbox--examples\"><header class=\"textbox__header\">\r\n<p class=\"textbox__title\">Eg 2b: How to read every line of a file? (a functional example)<\/p>\r\n\r\n<\/header>\r\n<div class=\"textbox__content\">\r\n<p class=\"c12\"><span class=\"c3\">To have redirection apply to the entire duration of the while loop and not just the read command, it is necessary to establish redirection and associate it with the while loop.<\/span><\/p>\r\n<p class=\"c12\"><span class=\"c3\">Here is a template which you can use to read lines from a file one-at-a-time.<\/span><\/p>\r\n\r\n<pre id=\"h.w0yrw0dvpa07\" class=\"c2 c4 \" style=\"padding-left: 40px\"><span class=\"c0\">while read wholeline<\/span>\r\n<span class=\"c0\">do<\/span>\r\n<span class=\"c0\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0something $wholeline\u00a0\u00a0#e.g. echo $wholeline<\/span>\r\n<span class=\"c0\">done &lt; inputfile<\/span><\/pre>\r\n<p class=\"c12\"><span class=\"c3\">To make this work, one must position the redirection operator after the command to which it should be applied. \u00a0The correct position is after the while command, in particular the keyword done. \u00a0Unix is smart enough to look ahead for redirection when it starts executing the while command. \u00a0Any commands within the while command which draw from STDIN (e.g. read) will draw from the redirected file. (If you are having difficulty understanding redirection, refer to the chapter on Redirection and Pipes.)<\/span><\/p>\r\n\r\n<\/div>\r\n<\/div>\r\n&nbsp;\r\n<div class=\"textbox textbox--examples\"><header class=\"textbox__header\">\r\n<p class=\"textbox__title\">Eg 3: How to process multiple command line arguments (Method 1)<\/p>\r\n\r\n<\/header>\r\n<div class=\"textbox__content\">\r\n<p class=\"c12\">Recall that the cat command will accept <span class=\"c29\">any <\/span><span class=\"c3\">number of command line arguments like this: <\/span><\/p>\r\n\r\n<pre class=\"c12\" style=\"padding-left: 40px\"><span class=\"c0\">cat ch1 ch2 ch3 ...<\/span><\/pre>\r\n<p class=\"c12\">Let's say that you want to write your own Unix script that will accept any number of command line arguments<span class=\"c3\">: <\/span><\/p>\r\n\r\n<pre class=\"c12\" style=\"padding-left: 40px\"><span class=\"c26\">mycmd parm1 parm2 ...<\/span><\/pre>\r\n<p class=\"c12\"><span class=\"c3\">The key requirement here is that you will not know beforehand how many command-line arguments the user will supply when the user runs your program. \u00a0Your program must be flexible enough to handle zero or more command line arguments.<\/span><\/p>\r\n<p class=\"c12\"><span class=\"c3\">Here is a template of code to solve this problem:<\/span><\/p>\r\n\r\n<pre id=\"h.fu3ubjebea71\" class=\"c2 c4 \" style=\"padding-left: 40px\"><span class=\"c0\">while [ $1 ]<\/span>\r\n<span class=\"c0\">do<\/span>\r\n<span class=\"c0\"> \u00a0\u00a0someprocess $1<\/span>\r\n<span class=\"c0\"> \u00a0\u00a0shift<\/span>\r\n<span class=\"c0\">done<\/span><\/pre>\r\n<p class=\"c12\"><span class=\"c3\">Explanatory notes:<\/span><\/p>\r\n\r\n<ol class=\"c14 lst-kix_qkoa9ek4sumi-0 start\" start=\"1\">\r\n \t<li class=\"c12 c15 li-bullet-0\"><span class=\"c3\">The last command in the while list is a test command (using the left square bracket synonym).<\/span><\/li>\r\n \t<li class=\"c12 c15 li-bullet-0\"><span class=\"c3\">With no specific test operator (e.g. -r, etc.), the default behaviour of the test command is to check if the string is null. \u00a0The return code ($?) is TRUE for a non-null string (something is there), and FALSE for a null string.<\/span><\/li>\r\n \t<li class=\"c12 c15 li-bullet-0\"><span class=\"c3\">The shift command slides all command line parameters to the left, in this case moving the next unprocessed command line argument into the $1 position. \u00a0(The shift command also has the side-effect of decrementing the $# variable.)<\/span><\/li>\r\n \t<li class=\"c12 c15 li-bullet-0\"><span class=\"c3\">Once there are no more command line arguments, the loop terminates. \u00a0Note that if there were no command line arguments to begin with ($1 is NULL), the loop immediately terminates without entry into the body. \u00a0<\/span><\/li>\r\n<\/ol>\r\n<\/div>\r\n<\/div>\r\n&nbsp;\r\n<div class=\"textbox textbox--exercises\"><header class=\"textbox__header\">\r\n<p class=\"textbox__title\">Exercise: Password entry loop<\/p>\r\n\r\n<\/header>\r\n<div class=\"textbox__content\">\r\n<p class=\"c6\"><span class=\"c3\">Write a bash script which will continually prompt a user to enter a password matching the control flow in the example below. \u00a0If the user enters the correct password, grant access. \u00a0If the user enters the wrong password, give the user another try (unlimited). \u00a0Avoid code duplication.<\/span><\/p>\r\n<p class=\"c6\"><span class=\"c3\">Sample dialog:<\/span><\/p>\r\n\r\n<pre id=\"h.f96okg636qf0\" class=\"c6 c20 \">$ <strong><span class=\"c31\">.\/whilecmdlist<\/span><\/strong>\r\nWhat is the password? <strong><span class=\"c31\">happy<\/span><\/strong>\r\n<span class=\"c0\">--- Sorry, that is not the right password.<\/span>\r\nWhat is the password? <strong><span class=\"c31\">access<\/span><\/strong>\r\n<span class=\"c0\">--- Sorry, that is not the right password.<\/span>\r\nWhat is the password? <strong><span class=\"c31\">Strawberry<\/span><\/strong>\r\n<span class=\"c0\">Welcome to the system.<\/span>\r\n<span class=\"c0\">$<\/span><\/pre>\r\n<\/div>\r\n<\/div>\r\n<h2 id=\"h.ob9kkrkl6hkn\" class=\"c21 c4\"><span class=\"c25\">until command<\/span><\/h2>\r\n<p class=\"c12\"><span class=\"c3\">The until command in Unix implements the General Loop construct discussed earlier.<\/span><\/p>\r\n<p class=\"c12\"><span class=\"c3\">The syntax is identical to the while command with the only difference that the logic of the conditional test is reversed. \u00a0If the last command in list1 is TRUE, the loop terminates.<\/span><\/p>\r\n<p class=\"c12\"><span class=\"c3\">Here's the same explanation for the until command:<\/span><\/p>\r\n<p class=\"c12\">Unix runs the <span class=\"c28\"><span style=\"text-decoration: underline\">list1<\/span> <\/span>and looks at the return code ($?) of the <span class=\"c35\">last command <\/span>in <span class=\"c28\">list.<\/span>\u00a0 (If there's only one command in the <span style=\"text-decoration: underline\"><span class=\"c28\">list1<\/span><\/span>, then that's the last command.) \u00a0If the return code of this last command is FALSE, then <span style=\"text-decoration: underline\"><span class=\"c28\">list2 <\/span><\/span>(loop body) is run. \u00a0Once the keyword \"done\" is reached, control returns to re-run <span style=\"text-decoration: underline\"><span class=\"c28\">list1 <\/span><\/span>and the return code of the last command in <span class=\"c28\"><span style=\"text-decoration: underline\">list1<\/span> <\/span><span class=\"c3\">is checked. \u00a0 If the return code of this last command is TRUE, then the until loop terminates, and control continues after the keyword \"done\".<\/span><\/p>\r\n\r\n<h2 id=\"h.6jshw475ix66\" class=\"c4 c24\"><span class=\"c25\">for command<\/span><\/h2>\r\n<p class=\"c12\"><span class=\"c3\">The for command in Unix operates quite differently than what is common in other computer languages like BASIC or Pascal. \u00a0In particular, it is not a counted loop, rather one should think \"iterative substitution\".<\/span><\/p>\r\n\r\n<div class=\"textbox\">\r\n<p class=\"c12\"><span class=\"c3\">Syntax:<\/span><span class=\"c3\">\u00a0<\/span><\/p>\r\n\r\n<pre id=\"h.ktj1na1jagj9\" class=\"c2 c4\" style=\"padding-left: 40px\"><span class=\"c0\">for name in word ...<\/span>\r\n<span class=\"c0\">do<\/span>\r\n<span class=\"c0\">  \u00a0list<\/span>\r\n<span class=\"c0\">done<\/span><\/pre>\r\n<p class=\"c12\"><span class=\"c3\">The list of words is expanded to create a list of items. \u00a0Each of these items is substituted into the variable name one-at-a-time and the list is run.<\/span><\/p>\r\n\r\n<\/div>\r\n&nbsp;\r\n<div class=\"textbox textbox--examples\"><header class=\"textbox__header\">\r\n<p class=\"textbox__title\">E.g. 1: Multiple file rename problem<\/p>\r\n\r\n<\/header>\r\n<div class=\"textbox__content\">\r\n<p class=\"c6\"><span class=\"c3\">Consider the problem of adding a suffix to several files.<\/span><\/p>\r\n<p class=\"c6\"><span class=\"c3\">Those familiar with a DOS or Windows Power Shell environment could use a command like this:<\/span><\/p>\r\n\r\n<pre id=\"h.wdrp2n6pkw9r\" class=\"c2 c4\" style=\"padding-left: 40px\"><span class=\"c0\">ren assig* assig*.bak<\/span><\/pre>\r\n<p class=\"c6\"><span class=\"c3\">Unfortunately a similar command does not work in Unix:<\/span><\/p>\r\n\r\n<pre id=\"h.hpp6ihrn25ql\" class=\"c2 c4\" style=\"padding-left: 40px\"><span class=\"c0\">mv assig* assig*.backup<\/span><\/pre>\r\n<p class=\"c12\"><span class=\"c3\">Nevertheless, this problem can be solved with the simple use of a for command:<\/span><\/p>\r\n\r\n<pre id=\"h.m89rbotpzgoo\" class=\"c2 c4\" style=\"padding-left: 40px\"><span class=\"c0\">for filevar in assig*<\/span>\r\n<span class=\"c0\">do<\/span>\r\n<span class=\"c0\">  \u00a0mv ${filevar} ${filevar}.backup<\/span>\r\n<span class=\"c0\">done<\/span><span class=\"c3\">\u00a0<\/span><\/pre>\r\n<p class=\"c6\"><span class=\"c3\">Explanatory notes<\/span><\/p>\r\n\r\n<ol class=\"c14 lst-kix_jxjz15y3u0r4-0 start\" start=\"1\">\r\n \t<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">The word list is the wildcard file specification assig*. \u00a0On expansion, for the purposes of this example, assume that the result is three matching files: assig1, assig2, assig3.<\/span><\/li>\r\n \t<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">Each of these words is substituted one-at-a-time in the loop variable filevar, and the body of the loop is run (code between do and done).<\/span><\/li>\r\n \t<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">When the body of the loop is run with the variable substitutions, the effective commands generated are:<\/span><\/li>\r\n<\/ol>\r\n<ul class=\"c14 lst-kix_jxjz15y3u0r4-1 start\">\r\n \t<li id=\"h.dvkx6p67ti2p\" class=\"c2 c4 c34 li-bullet-0\">\r\n<pre>mv assig1 assig1.backup<\/pre>\r\n<\/li>\r\n \t<li id=\"h.of4e4gyjmtnb\" class=\"c2 c4 c34 li-bullet-0\">\r\n<pre>mv assig2 assig2.backup<\/pre>\r\n<\/li>\r\n \t<li id=\"h.v1za48sl0iwa\" class=\"c2 c4 c34 li-bullet-0\">\r\n<pre>mv assig3 assig3.backup<\/pre>\r\n<\/li>\r\n<\/ul>\r\n<p class=\"c6\"><span class=\"c3\">Additional Notes<\/span><\/p>\r\n\r\n<ol class=\"c14 lst-kix_kt3z4gamf8jx-0 start\" start=\"1\">\r\n \t<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">There is no specific string concatenation operator in Unix. \u00a0Simply placing text adjacently achieves the desired result. \u00a0Here the string \".backup\" is appended to the \"assig1\", etc.<\/span><\/li>\r\n \t<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">As stated earlier, while the use of set braces { } is often omitted when spelling variables, here it helps to clarify the distinction between the variable name and surrounding text.<\/span><\/li>\r\n<\/ol>\r\n<\/div>\r\n<\/div>\r\n&nbsp;\r\n<div class=\"textbox textbox--examples\"><header class=\"textbox__header\">\r\n<p class=\"textbox__title\">E.g. 2: Count the number of entries in a directory<\/p>\r\n\r\n<\/header>\r\n<div class=\"textbox__content\">\r\n<p class=\"c6\"><span class=\"c3\">Print the number of entries in the \/etc directory.<\/span><\/p>\r\n\r\n<pre id=\"h.sp9gj6lahn0n\" class=\"c2 c4\" style=\"padding-left: 40px\"><span class=\"c0\">cou<\/span><span class=\"c0\">nt=0<\/span>\r\n<span class=\"c0\">for files in $(ls \/etc)<\/span>\r\n<span class=\"c0\">do<\/span>\r\n<span class=\"c0\"> \u00a0\u00a0count=$(( $count +1 ))<\/span>\r\n<span class=\"c0\">done<\/span>\r\n<span class=\"c0\">echo The number of entries is $count<\/span><span class=\"c3\">\u00a0<\/span><\/pre>\r\n<p class=\"c6\"><span class=\"c3\">Explanatory notes<\/span><\/p>\r\n\r\n<ol class=\"c14 lst-kix_qu9z8afik2yp-0 start\" start=\"1\">\r\n \t<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">The word list consists of a command substitution call of the ls command on the \/etc directory.<\/span><\/li>\r\n \t<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">Expansion of this word list is the names of the entries in the \/etc directory.<\/span><\/li>\r\n \t<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">Each name is substituted into the loop variable files.<\/span><\/li>\r\n \t<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">The body of the loop consists of incrementing a counter which will eventually hold the number of entries.<\/span><\/li>\r\n \t<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">Outside the loop, the total number of entries is printed in an appropriate message.<\/span><\/li>\r\n<\/ol>\r\n<\/div>\r\n<\/div>\r\n&nbsp;\r\n<p class=\"c5\"><span class=\"c3\">\u00a0<\/span><\/p>\r\n\r\n<div class=\"textbox textbox--examples\"><header class=\"textbox__header\">\r\n<p class=\"textbox__title\">Eg 3: How to process multiple command line arguments (Method 2)<\/p>\r\n\r\n<\/header>\r\n<div class=\"textbox__content\">\r\n<p class=\"c12\"><span class=\"c3\">Let's say that you want to write your own Unix script that will accept any number of command line arguments: <\/span><\/p>\r\n\r\n<pre id=\"h.eb42c7begfsf\" class=\"c2 c4\" style=\"padding-left: 40px\"><span class=\"c0\">mycmd parm1 parm2 \u2026<\/span><\/pre>\r\n<p class=\"c6\"><span class=\"c3\">Here is another method, this one using the for command<\/span><\/p>\r\n\r\n<pre id=\"h.8jw8rq3v95u2\" class=\"c2 c4\" style=\"padding-left: 40px\"><span class=\"c0\">for cmdarg in $*<\/span>\r\n<span class=\"c0\">do<\/span>\r\n<span class=\"c0\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0myprocess $cmdarg<\/span>\r\n<span class=\"c0\">done<\/span>\r\n<span class=\"c3\"> <\/span><\/pre>\r\n<p class=\"c6\"><span class=\"c3\">Explanatory notes<\/span><\/p>\r\n\r\n<ol class=\"c14 lst-kix_rnczc8u62bow-0 start\" start=\"1\">\r\n \t<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">The special variable $* expands to all command line arguments present starting with $1.<\/span><\/li>\r\n \t<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">Each of these words is substituted into the loop variable cmdarg.<\/span><\/li>\r\n \t<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">The body of the loop is run against each command line argument (myprocess is a fictitious Unix command or script).<\/span><\/li>\r\n \t<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">Unlike the while command example, there is no need for the shift command.<\/span><\/li>\r\n \t<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">The value of $# is intact after the completion of the for loop.<\/span><\/li>\r\n<\/ol>\r\n&nbsp;\r\n\r\n<\/div>\r\n<\/div>\r\n<h2>break<\/h2>\r\nThe break statement causes control to exit the current for, while, or until loop.\u00a0 One must be judicious in its use to avoid \"spaghetti\" code.\u00a0 Except in rare circumstances, adherence to structure programming principles leads to clearer and less error-prone code. In many cases the use of break should be avoided.\r\n<table class=\"grid\" style=\"border-collapse: collapse;width: 100%;height: 45px\" border=\"0\">\r\n<tbody>\r\n<tr style=\"height: 15px\">\r\n<td style=\"width: 50%;height: 15px\">\r\n<h4>Poor (and rampant) use of break<\/h4>\r\n<\/td>\r\n<td style=\"width: 50%;height: 15px\">\r\n<h4>Better code<\/h4>\r\n<\/td>\r\n<\/tr>\r\n<tr style=\"height: 15px\">\r\n<td style=\"width: 50%;height: 15px;vertical-align: top\">\r\n<pre>i=0\r\nwhile true\r\ndo\r\n  if [ $i -eq 5 ]\r\n  then\r\n    echo Reached 5\r\n    <strong>break<\/strong>\r\n  fi\r\n  i=$(( $i + 1 ))\r\ndone<\/pre>\r\n<\/td>\r\n<td style=\"width: 50%;height: 15px;text-align: left;vertical-align: top\">\r\n<pre>i=0\r\nwhile [ $i -ne 5 ]\r\ndo\r\n  i=$(( $i + 1 ))\r\ndone\r\necho Reached 5<\/pre>\r\n<\/td>\r\n<\/tr>\r\n<tr style=\"height: 15px\">\r\n<td style=\"width: 50%;height: 15px;vertical-align: top\">\r\n<h4>Observations<\/h4>\r\nThe use of an infinite loop (while true) is generally unnecessary (except perhaps in server processes) and should be avoided because it requires unstructured exit points leading to \"spaghetti\" code.\u00a0 Here, the break statement is used to jump out midway through the loop contributing to poor program structure.\r\n\r\n&nbsp;<\/td>\r\n<td style=\"width: 50%;height: 15px;vertical-align: top\">\r\n<h4>Observations<\/h4>\r\nThe purpose and limit of the loop is coded clearly in the while condition.\u00a0 Termination of the loop naturally follows coded instructions.\u00a0 There is no jumping out from the middle of the loop.\u00a0 The code is clearer, shorter, and better structured with a single entry and exit point for the loop.<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;","rendered":"<p class=\"c13 c21\"><span class=\"c3\">Eventually you will need to repeat actions. \u00a0A practical way to repeat actions without repeating code is achieved with the construct of a loop. <\/span><\/p>\n<div class=\"textbox shaded\">\n<h1 id=\"h.32v1nhr0jhri\" class=\"c21 c4\"><span class=\"c25\">Computer Science Loop Concepts<\/span><\/h1>\n<p class=\"c6\"><span class=\"c3\">The construct of a loop allows for the repetition of actions without repeating code. \u00a0<\/span><\/p>\n<h2 class=\"c5\"><span class=\"c11\">General Loop (Concept)<\/span><\/h2>\n<p class=\"c6\"><span class=\"c3\">The most general flow of control is illustrated in the flowchart below (General). \u00a0Observe that there can be a set of actions (Block 1) which is performed regardless on entry into the loop. \u00a0Then a conditional check is made to determine continuation of the loop. \u00a0If satisfied, further actions within the loop (Block 2) will be performed. \u00a0<\/span><\/p>\n<p class=\"c5\"><span class=\"c3\">\u00a0<\/span><\/p>\n<p class=\"c6\">Not all computer languages support the General loop construct (Unix<br \/>\n<span class=\"c29\">does <\/span><span class=\"c3\">support a General loop). \u00a0If a computer language does not support the General loop construct, it will usually support one or more of the specific cases of the General loop. \u00a0For example, C Language supports both a While and a Do-While construct but not the General loop construct.<\/span><\/p>\n<h2 id=\"h.s7we9xpua7pz\" class=\"c4 c7\"><span class=\"c11\">While Loop (Concept)<\/span><\/h2>\n<p class=\"c6\"><span class=\"c3\">The While Loop, as a concept, is a special case of a General Loop which has no actions for Block 1. \u00a0In this case, the condition is checked as a first action on entry into the loop construct. \u00a0See flowchart below (While).<\/span><\/p>\n<h2 id=\"h.ds9d1tv3r7e2\" class=\"c7 c4\"><span class=\"c11\">Do-While Loop (Concept)<\/span><\/h2>\n<p class=\"c6\"><span class=\"c3\">The Do-While Loop, as a concept, is a special case of a General Loop which has no actions for Block 2. \u00a0In this case, the Block 1 code is executed on entry into the loop construct, and the condition is checked at the end of the loop construct. \u00a0See flowchart below (Do-While).<\/span><\/p>\n<table class=\"grid\">\n<thead>\n<tr class=\"c18\">\n<th class=\"c8\" colspan=\"1\" rowspan=\"1\" scope=\"col\">\n<p class=\"c2\"><span class=\"c3\">General<\/span><\/p>\n<\/th>\n<th class=\"c8\" colspan=\"1\" rowspan=\"1\" scope=\"col\">\n<p class=\"c2\"><span class=\"c3\">While<\/span><\/p>\n<\/th>\n<th class=\"c8\" colspan=\"1\" rowspan=\"1\" scope=\"col\">\n<p class=\"c2\"><span class=\"c3\">Do-While<\/span><\/p>\n<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr class=\"c32\">\n<td class=\"c8\" colspan=\"1\" rowspan=\"1\">\n<p class=\"c2\"><span style=\"overflow: hidden;margin: 0.00px 0.00px;border: 0.00px solid #000000;width: 178.50px;height: 264.67px\"><br \/>\n<img loading=\"lazy\" decoding=\"async\" alt=\"General loop has arrow to block 1 before decision and block 2 after decision; decision has 2 arrows out with true path leading to block 2 and false path leaving loop; arrow from block 2 loops back to block 1.\" src=\"http:\/\/pressbooks.library.ryerson.ca\/opsyshiraki\/wp-content\/uploads\/sites\/330\/2022\/03\/image3-1.png\" style=\"width: 178.50px;height: 264.67px;margin-left: 0.00px;margin-top: 0.00px\" width=\"178\" height=\"257\" class=\"alignnone\" \/><br \/>\n<\/span><\/p>\n<\/td>\n<td class=\"c8\" colspan=\"1\" rowspan=\"1\">\n<p class=\"c2\"><span style=\"overflow: hidden;margin: 0.00px 0.00px;border: 0.00px solid #000000;width: 181.50px;height: 269.12px\"><br \/>\n<img loading=\"lazy\" decoding=\"async\" alt=\"While has arrow to decision (no block 1) with arrow to block 2 then arrow from block 2 loops back to decision.\" src=\"http:\/\/pressbooks.library.ryerson.ca\/opsyshiraki\/wp-content\/uploads\/sites\/330\/2022\/03\/image2-1.png\" style=\"width: 181.50px;height: 269.12px;margin-left: 0.00px;margin-top: 0.00px\" width=\"181\" height=\"262\" class=\"alignnone\" \/><br \/>\n<\/span><\/p>\n<\/td>\n<td class=\"c8\" colspan=\"1\" rowspan=\"1\">\n<p class=\"c2\"><span style=\"overflow: hidden;margin: 0.00px 0.00px;border: 0.00px solid #000000;width: 190.99px;height: 276.44px\"><br \/>\n<img loading=\"lazy\" decoding=\"async\" alt=\"Do-while has has arrow to block 1 before decision (no block2). True output of decision box loops back to block 1.\" src=\"http:\/\/pressbooks.library.ryerson.ca\/opsyshiraki\/wp-content\/uploads\/sites\/330\/2022\/03\/image1-2.png\" style=\"width: 190.99px;height: 276.44px;margin-left: 0.00px;margin-top: 0.00px\" width=\"190\" height=\"268\" class=\"alignnone\" \/><br \/>\n<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"c21 c13 c9\"><span class=\"c3\">\u00a0<\/span><\/p>\n<\/div>\n<p><span><\/p>\n<div id=\"h5p-3\">\n<div class=\"h5p-iframe-wrapper\"><iframe id=\"h5p-iframe-3\" class=\"h5p-iframe\" data-content-id=\"3\" style=\"height:1px\" src=\"about:blank\" frameBorder=\"0\" scrolling=\"no\" title=\"Compsci while\"><\/iframe><\/div>\n<\/div>\n<p><\/span><\/p>\n<p><span><\/p>\n<div id=\"h5p-4\">\n<div class=\"h5p-iframe-wrapper\"><iframe id=\"h5p-iframe-4\" class=\"h5p-iframe\" data-content-id=\"4\" style=\"height:1px\" src=\"about:blank\" frameBorder=\"0\" scrolling=\"no\" title=\"Compsci do-while\"><\/iframe><\/div>\n<\/div>\n<p><\/span><\/p>\n<h1 id=\"h.v5u5941k43y1\" class=\"c21 c4\"><span class=\"c30\">Loops in Unix<\/span><\/h1>\n<p class=\"c6\">There are three commands in Unix to support loops.<\/p>\n<ul class=\"c14 lst-kix_r52uojy9d8wy-0 start\">\n<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">while<\/span><\/li>\n<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">until\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/li>\n<li class=\"c6 c15 li-bullet-0\">for<\/li>\n<\/ul>\n<h2 id=\"h.1g17zi2rpgp\" class=\"c21 c4\"><span class=\"c25\">while command<\/span><\/h2>\n<p class=\"c12\"><span class=\"c3\">The while command in Unix implements the General Loop construct discussed earlier.<\/span><\/p>\n<p>&nbsp;<\/p>\n<table class=\"grid\">\n<thead>\n<tr class=\"c18\">\n<th class=\"c17\" scope=\"col\">\n<p class=\"c12\"><span class=\"c3\">general syntax<\/span><\/p>\n<\/th>\n<th class=\"c17\" scope=\"col\">\n<p class=\"c2\"><span class=\"c11\">relation to flow chart<\/span><\/p>\n<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n<pre class=\"c12\" style=\"margin-top: 1em\"><span class=\"c0\">while list1<\/span>\r\n<span class=\"c0\">do<\/span>\r\n<span class=\"c0\">  list2<\/span>\r\n<span class=\"c26\">done\r\n<\/span><\/pre>\n<\/td>\n<td>\n<pre id=\"h.4nau2ymu2e5j\" class=\"c2 c20\" style=\"margin-top: 1em\"><span class=\"c0\">while block1<\/span>\r\n<span class=\"c0\">  \u00a0 \u00a0 check<\/span>\r\n<span class=\"c0\">do<\/span>\r\n<span class=\"c0\">  \u00a0block 2<\/span>\r\n<span class=\"c0\">done\r\n<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"c12\">Unix runs the <span style=\"text-decoration: underline\"><span class=\"c28\">list1 <\/span><\/span>and looks at the return code ($?) of the <span class=\"c35\">last command <\/span>in <span class=\"c28\">list.<\/span>\u00a0 (If there&#8217;s only one command in the <span style=\"text-decoration: underline\"><span class=\"c28\">list1<\/span><\/span>, then that&#8217;s the last command.) \u00a0If the return code of this last command is TRUE, then <span class=\"c28\"><span style=\"text-decoration: underline\">list2<\/span> <\/span>(loop body) is run. \u00a0Once the keyword &#8220;done&#8221; is reached, control returns to re-run <span class=\"c28\"><span style=\"text-decoration: underline\">list1<\/span> <\/span>and the return code of the last command in <span class=\"c28\"><span style=\"text-decoration: underline\">list1<\/span> <\/span><span class=\"c3\">is checked. \u00a0 If the return code of this last command is FALSE, then the while loop terminates, and control continues after the keyword &#8220;done&#8221;.<\/span><\/p>\n<p>&nbsp;<\/p>\n<div class=\"textbox textbox--examples\">\n<header class=\"textbox__header\">\n<p class=\"textbox__title\">E.g. 1: Count from 1 to 12<\/p>\n<\/header>\n<div class=\"textbox__content\">\n<pre class=\"c2 c13\"><span class=\"c0\">month=1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span>\r\n<span class=\"c0\">while echo Checking limit against month ${month}<\/span>\r\n<span class=\"c26\">  [ ${month} -le 12 ]\u00a0\u00a0\u00a0# The test is the last command in list1<\/span>\r\n<span class=\"c0\">do<\/span>\r\n<span class=\"c26\">  \u00a0echo Performing action for month ${month}<\/span>\r\n<span class=\"c26\">  \u00a0month=$(( ${month} + 1))<\/span>\r\n<span class=\"c26\">done<\/span><span class=\"c3\">\u00a0\r\necho Value of month outside loop is ${month}<\/span><\/pre>\n<h3 class=\"c33 c4\" id=\"h.mjqovltwcnos\">Notes and observations<\/h3>\n<ol class=\"c14 lst-kix_oax79x4ju1qf-0 start\" start=\"1\">\n<li class=\"c12 c15 li-bullet-0\"><span class=\"c3\">The last command of list1 is a test command using the left bracket synonym.<\/span><\/li>\n<li class=\"c12 c15 li-bullet-0\"><span class=\"c3\">The -le is a test command option for the numeric comparison less than or equal to.<\/span><\/li>\n<li class=\"c12 c15 li-bullet-0\"><span class=\"c3\">As Unix program variables hold strings, to perform arithmetic operations, one must use the $(( .. )) syntax with dollar sign and double parentheses.<\/span><\/li>\n<li class=\"c12 c15 li-bullet-0\"><span class=\"c3\">The formal spelling of Unix variables requires enclosure in set braces { }. \u00a0Where there is no ambiguity, it is common practice in Unix to omit the set braces.<\/span><\/li>\n<li>Try running this code and see that the value of month never exceeds 12 in the line &#8220;echo Performing action &#8230;&#8221;.<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<h3 class=\"c7 c4\" id=\"h.iodnffrfpn6n\"><span class=\"c11\">Preamble: Redirecting input from a file<\/span><\/h3>\n<pre id=\"h.db51xr7z9zar\" class=\"c2 c4\"><span class=\"c0\">$ <strong>cat novel<\/strong><\/span>\r\n<span class=\"c0\">It was a<\/span>\r\n<span class=\"c0\">dark and<\/span>\r\n<span class=\"c0\">stormy<\/span>\r\n<span class=\"c0\">$ <strong>read oneline &lt; novel<\/strong><\/span>\r\n<span class=\"c0\">$ <strong>echo $oneline<\/strong><\/span>\r\n<span class=\"c0\">It was a<\/span>\r\n<span class=\"c0\">$ <strong>read oneline &lt; novel<\/strong><\/span>\r\n<span class=\"c0\">$ <strong>echo $oneline<\/strong><\/span>\r\n<span class=\"c0\">It was a<\/span>\r\n<span class=\"c0\">$<\/span><\/pre>\n<p class=\"c5\"><span class=\"c3\">\u00a0<\/span><\/p>\n<p class=\"c6\">Be reminded that redirection operators in Unix (&lt;, &gt;, etc.) apply only to the current command. \u00a0Once the command is over, redirection is restored to the default configuration (STDIN from keyboard, STDOUT to screen, etc.). \u00a0 Thus, observe that each time the read command is run, redirection is applied anew, and the first line of the file is read in.<\/p>\n<div class=\"textbox textbox--examples\">\n<header class=\"textbox__header\">\n<p class=\"textbox__title\">Eg 2a: How to read every line of a file (a non-functional example)<\/p>\n<\/header>\n<div class=\"textbox__content\" style=\"text-align: left\">\n<p class=\"c12\">The objective is to read a file one line at a time. \u00a0Here is a newbie&#8217;s first attempt.<\/p>\n<p class=\"c12 c9\"><span class=\"c3\">\u00a0<\/span><\/p>\n<pre id=\"h.cuirxscuuipk\" class=\"c2 c4\"><span class=\"c0\">$ <strong>cat badwhile<\/strong><\/span>\r\n<span class=\"c0\">while read wholeline &lt; novel<\/span>\r\n<span class=\"c0\">do<\/span>\r\n<span class=\"c0\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0something $wholeline \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#e.g. echo $wholeline<\/span>\r\n<span class=\"c0\">done<\/span>\r\n<span class=\"c0\">$ <strong>.\/badwhile<\/strong><\/span>\r\n<span class=\"c0\">It was a<\/span>\r\n<span class=\"c0\">It was a<\/span>\r\n<span class=\"c0\">It was a<\/span>\r\n<span class=\"c0\">It was a<\/span>\r\n...\r\n\r\n<span class=\"c3\">\r\n<\/span><span class=\"c3\"><\/span><\/pre>\n<p>What happened? \u00a0The redirection operator applies to the read command. \u00a0Each time the read command is run, redirection is applied anew and only the first line of the file is ever read.<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>How can one fix this problem?<\/p>\n<div class=\"textbox textbox--examples\">\n<header class=\"textbox__header\">\n<p class=\"textbox__title\">Eg 2b: How to read every line of a file? (a functional example)<\/p>\n<\/header>\n<div class=\"textbox__content\">\n<p class=\"c12\"><span class=\"c3\">To have redirection apply to the entire duration of the while loop and not just the read command, it is necessary to establish redirection and associate it with the while loop.<\/span><\/p>\n<p class=\"c12\"><span class=\"c3\">Here is a template which you can use to read lines from a file one-at-a-time.<\/span><\/p>\n<pre id=\"h.w0yrw0dvpa07\" class=\"c2 c4\" style=\"padding-left: 40px\"><span class=\"c0\">while read wholeline<\/span>\r\n<span class=\"c0\">do<\/span>\r\n<span class=\"c0\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0something $wholeline\u00a0\u00a0#e.g. echo $wholeline<\/span>\r\n<span class=\"c0\">done &lt; inputfile<\/span><\/pre>\n<p class=\"c12\"><span class=\"c3\">To make this work, one must position the redirection operator after the command to which it should be applied. \u00a0The correct position is after the while command, in particular the keyword done. \u00a0Unix is smart enough to look ahead for redirection when it starts executing the while command. \u00a0Any commands within the while command which draw from STDIN (e.g. read) will draw from the redirected file. (If you are having difficulty understanding redirection, refer to the chapter on Redirection and Pipes.)<\/span><\/p>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"textbox textbox--examples\">\n<header class=\"textbox__header\">\n<p class=\"textbox__title\">Eg 3: How to process multiple command line arguments (Method 1)<\/p>\n<\/header>\n<div class=\"textbox__content\">\n<p class=\"c12\">Recall that the cat command will accept <span class=\"c29\">any <\/span><span class=\"c3\">number of command line arguments like this: <\/span><\/p>\n<pre class=\"c12\" style=\"padding-left: 40px\"><span class=\"c0\">cat ch1 ch2 ch3 ...<\/span><\/pre>\n<p class=\"c12\">Let&#8217;s say that you want to write your own Unix script that will accept any number of command line arguments<span class=\"c3\">: <\/span><\/p>\n<pre class=\"c12\" style=\"padding-left: 40px\"><span class=\"c26\">mycmd parm1 parm2 ...<\/span><\/pre>\n<p class=\"c12\"><span class=\"c3\">The key requirement here is that you will not know beforehand how many command-line arguments the user will supply when the user runs your program. \u00a0Your program must be flexible enough to handle zero or more command line arguments.<\/span><\/p>\n<p class=\"c12\"><span class=\"c3\">Here is a template of code to solve this problem:<\/span><\/p>\n<pre id=\"h.fu3ubjebea71\" class=\"c2 c4\" style=\"padding-left: 40px\"><span class=\"c0\">while [ $1 ]<\/span>\r\n<span class=\"c0\">do<\/span>\r\n<span class=\"c0\"> \u00a0\u00a0someprocess $1<\/span>\r\n<span class=\"c0\"> \u00a0\u00a0shift<\/span>\r\n<span class=\"c0\">done<\/span><\/pre>\n<p class=\"c12\"><span class=\"c3\">Explanatory notes:<\/span><\/p>\n<ol class=\"c14 lst-kix_qkoa9ek4sumi-0 start\" start=\"1\">\n<li class=\"c12 c15 li-bullet-0\"><span class=\"c3\">The last command in the while list is a test command (using the left square bracket synonym).<\/span><\/li>\n<li class=\"c12 c15 li-bullet-0\"><span class=\"c3\">With no specific test operator (e.g. -r, etc.), the default behaviour of the test command is to check if the string is null. \u00a0The return code ($?) is TRUE for a non-null string (something is there), and FALSE for a null string.<\/span><\/li>\n<li class=\"c12 c15 li-bullet-0\"><span class=\"c3\">The shift command slides all command line parameters to the left, in this case moving the next unprocessed command line argument into the $1 position. \u00a0(The shift command also has the side-effect of decrementing the $# variable.)<\/span><\/li>\n<li class=\"c12 c15 li-bullet-0\"><span class=\"c3\">Once there are no more command line arguments, the loop terminates. \u00a0Note that if there were no command line arguments to begin with ($1 is NULL), the loop immediately terminates without entry into the body. \u00a0<\/span><\/li>\n<\/ol>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"textbox textbox--exercises\">\n<header class=\"textbox__header\">\n<p class=\"textbox__title\">Exercise: Password entry loop<\/p>\n<\/header>\n<div class=\"textbox__content\">\n<p class=\"c6\"><span class=\"c3\">Write a bash script which will continually prompt a user to enter a password matching the control flow in the example below. \u00a0If the user enters the correct password, grant access. \u00a0If the user enters the wrong password, give the user another try (unlimited). \u00a0Avoid code duplication.<\/span><\/p>\n<p class=\"c6\"><span class=\"c3\">Sample dialog:<\/span><\/p>\n<pre id=\"h.f96okg636qf0\" class=\"c6 c20\">$ <strong><span class=\"c31\">.\/whilecmdlist<\/span><\/strong>\r\nWhat is the password? <strong><span class=\"c31\">happy<\/span><\/strong>\r\n<span class=\"c0\">--- Sorry, that is not the right password.<\/span>\r\nWhat is the password? <strong><span class=\"c31\">access<\/span><\/strong>\r\n<span class=\"c0\">--- Sorry, that is not the right password.<\/span>\r\nWhat is the password? <strong><span class=\"c31\">Strawberry<\/span><\/strong>\r\n<span class=\"c0\">Welcome to the system.<\/span>\r\n<span class=\"c0\">$<\/span><\/pre>\n<\/div>\n<\/div>\n<h2 id=\"h.ob9kkrkl6hkn\" class=\"c21 c4\"><span class=\"c25\">until command<\/span><\/h2>\n<p class=\"c12\"><span class=\"c3\">The until command in Unix implements the General Loop construct discussed earlier.<\/span><\/p>\n<p class=\"c12\"><span class=\"c3\">The syntax is identical to the while command with the only difference that the logic of the conditional test is reversed. \u00a0If the last command in list1 is TRUE, the loop terminates.<\/span><\/p>\n<p class=\"c12\"><span class=\"c3\">Here&#8217;s the same explanation for the until command:<\/span><\/p>\n<p class=\"c12\">Unix runs the <span class=\"c28\"><span style=\"text-decoration: underline\">list1<\/span> <\/span>and looks at the return code ($?) of the <span class=\"c35\">last command <\/span>in <span class=\"c28\">list.<\/span>\u00a0 (If there&#8217;s only one command in the <span style=\"text-decoration: underline\"><span class=\"c28\">list1<\/span><\/span>, then that&#8217;s the last command.) \u00a0If the return code of this last command is FALSE, then <span style=\"text-decoration: underline\"><span class=\"c28\">list2 <\/span><\/span>(loop body) is run. \u00a0Once the keyword &#8220;done&#8221; is reached, control returns to re-run <span style=\"text-decoration: underline\"><span class=\"c28\">list1 <\/span><\/span>and the return code of the last command in <span class=\"c28\"><span style=\"text-decoration: underline\">list1<\/span> <\/span><span class=\"c3\">is checked. \u00a0 If the return code of this last command is TRUE, then the until loop terminates, and control continues after the keyword &#8220;done&#8221;.<\/span><\/p>\n<h2 id=\"h.6jshw475ix66\" class=\"c4 c24\"><span class=\"c25\">for command<\/span><\/h2>\n<p class=\"c12\"><span class=\"c3\">The for command in Unix operates quite differently than what is common in other computer languages like BASIC or Pascal. \u00a0In particular, it is not a counted loop, rather one should think &#8220;iterative substitution&#8221;.<\/span><\/p>\n<div class=\"textbox\">\n<p class=\"c12\"><span class=\"c3\">Syntax:<\/span><span class=\"c3\">\u00a0<\/span><\/p>\n<pre id=\"h.ktj1na1jagj9\" class=\"c2 c4\" style=\"padding-left: 40px\"><span class=\"c0\">for name in word ...<\/span>\r\n<span class=\"c0\">do<\/span>\r\n<span class=\"c0\">  \u00a0list<\/span>\r\n<span class=\"c0\">done<\/span><\/pre>\n<p class=\"c12\"><span class=\"c3\">The list of words is expanded to create a list of items. \u00a0Each of these items is substituted into the variable name one-at-a-time and the list is run.<\/span><\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"textbox textbox--examples\">\n<header class=\"textbox__header\">\n<p class=\"textbox__title\">E.g. 1: Multiple file rename problem<\/p>\n<\/header>\n<div class=\"textbox__content\">\n<p class=\"c6\"><span class=\"c3\">Consider the problem of adding a suffix to several files.<\/span><\/p>\n<p class=\"c6\"><span class=\"c3\">Those familiar with a DOS or Windows Power Shell environment could use a command like this:<\/span><\/p>\n<pre id=\"h.wdrp2n6pkw9r\" class=\"c2 c4\" style=\"padding-left: 40px\"><span class=\"c0\">ren assig* assig*.bak<\/span><\/pre>\n<p class=\"c6\"><span class=\"c3\">Unfortunately a similar command does not work in Unix:<\/span><\/p>\n<pre id=\"h.hpp6ihrn25ql\" class=\"c2 c4\" style=\"padding-left: 40px\"><span class=\"c0\">mv assig* assig*.backup<\/span><\/pre>\n<p class=\"c12\"><span class=\"c3\">Nevertheless, this problem can be solved with the simple use of a for command:<\/span><\/p>\n<pre id=\"h.m89rbotpzgoo\" class=\"c2 c4\" style=\"padding-left: 40px\"><span class=\"c0\">for filevar in assig*<\/span>\r\n<span class=\"c0\">do<\/span>\r\n<span class=\"c0\">  \u00a0mv ${filevar} ${filevar}.backup<\/span>\r\n<span class=\"c0\">done<\/span><span class=\"c3\">\u00a0<\/span><\/pre>\n<p class=\"c6\"><span class=\"c3\">Explanatory notes<\/span><\/p>\n<ol class=\"c14 lst-kix_jxjz15y3u0r4-0 start\" start=\"1\">\n<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">The word list is the wildcard file specification assig*. \u00a0On expansion, for the purposes of this example, assume that the result is three matching files: assig1, assig2, assig3.<\/span><\/li>\n<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">Each of these words is substituted one-at-a-time in the loop variable filevar, and the body of the loop is run (code between do and done).<\/span><\/li>\n<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">When the body of the loop is run with the variable substitutions, the effective commands generated are:<\/span><\/li>\n<\/ol>\n<ul class=\"c14 lst-kix_jxjz15y3u0r4-1 start\">\n<li id=\"h.dvkx6p67ti2p\" class=\"c2 c4 c34 li-bullet-0\">\n<pre>mv assig1 assig1.backup<\/pre>\n<\/li>\n<li id=\"h.of4e4gyjmtnb\" class=\"c2 c4 c34 li-bullet-0\">\n<pre>mv assig2 assig2.backup<\/pre>\n<\/li>\n<li id=\"h.v1za48sl0iwa\" class=\"c2 c4 c34 li-bullet-0\">\n<pre>mv assig3 assig3.backup<\/pre>\n<\/li>\n<\/ul>\n<p class=\"c6\"><span class=\"c3\">Additional Notes<\/span><\/p>\n<ol class=\"c14 lst-kix_kt3z4gamf8jx-0 start\" start=\"1\">\n<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">There is no specific string concatenation operator in Unix. \u00a0Simply placing text adjacently achieves the desired result. \u00a0Here the string &#8220;.backup&#8221; is appended to the &#8220;assig1&#8221;, etc.<\/span><\/li>\n<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">As stated earlier, while the use of set braces { } is often omitted when spelling variables, here it helps to clarify the distinction between the variable name and surrounding text.<\/span><\/li>\n<\/ol>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"textbox textbox--examples\">\n<header class=\"textbox__header\">\n<p class=\"textbox__title\">E.g. 2: Count the number of entries in a directory<\/p>\n<\/header>\n<div class=\"textbox__content\">\n<p class=\"c6\"><span class=\"c3\">Print the number of entries in the \/etc directory.<\/span><\/p>\n<pre id=\"h.sp9gj6lahn0n\" class=\"c2 c4\" style=\"padding-left: 40px\"><span class=\"c0\">cou<\/span><span class=\"c0\">nt=0<\/span>\r\n<span class=\"c0\">for files in $(ls \/etc)<\/span>\r\n<span class=\"c0\">do<\/span>\r\n<span class=\"c0\"> \u00a0\u00a0count=$(( $count +1 ))<\/span>\r\n<span class=\"c0\">done<\/span>\r\n<span class=\"c0\">echo The number of entries is $count<\/span><span class=\"c3\">\u00a0<\/span><\/pre>\n<p class=\"c6\"><span class=\"c3\">Explanatory notes<\/span><\/p>\n<ol class=\"c14 lst-kix_qu9z8afik2yp-0 start\" start=\"1\">\n<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">The word list consists of a command substitution call of the ls command on the \/etc directory.<\/span><\/li>\n<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">Expansion of this word list is the names of the entries in the \/etc directory.<\/span><\/li>\n<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">Each name is substituted into the loop variable files.<\/span><\/li>\n<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">The body of the loop consists of incrementing a counter which will eventually hold the number of entries.<\/span><\/li>\n<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">Outside the loop, the total number of entries is printed in an appropriate message.<\/span><\/li>\n<\/ol>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p class=\"c5\"><span class=\"c3\">\u00a0<\/span><\/p>\n<div class=\"textbox textbox--examples\">\n<header class=\"textbox__header\">\n<p class=\"textbox__title\">Eg 3: How to process multiple command line arguments (Method 2)<\/p>\n<\/header>\n<div class=\"textbox__content\">\n<p class=\"c12\"><span class=\"c3\">Let&#8217;s say that you want to write your own Unix script that will accept any number of command line arguments: <\/span><\/p>\n<pre id=\"h.eb42c7begfsf\" class=\"c2 c4\" style=\"padding-left: 40px\"><span class=\"c0\">mycmd parm1 parm2 \u2026<\/span><\/pre>\n<p class=\"c6\"><span class=\"c3\">Here is another method, this one using the for command<\/span><\/p>\n<pre id=\"h.8jw8rq3v95u2\" class=\"c2 c4\" style=\"padding-left: 40px\"><span class=\"c0\">for cmdarg in $*<\/span>\r\n<span class=\"c0\">do<\/span>\r\n<span class=\"c0\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0myprocess $cmdarg<\/span>\r\n<span class=\"c0\">done<\/span>\r\n<span class=\"c3\"> <\/span><\/pre>\n<p class=\"c6\"><span class=\"c3\">Explanatory notes<\/span><\/p>\n<ol class=\"c14 lst-kix_rnczc8u62bow-0 start\" start=\"1\">\n<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">The special variable $* expands to all command line arguments present starting with $1.<\/span><\/li>\n<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">Each of these words is substituted into the loop variable cmdarg.<\/span><\/li>\n<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">The body of the loop is run against each command line argument (myprocess is a fictitious Unix command or script).<\/span><\/li>\n<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">Unlike the while command example, there is no need for the shift command.<\/span><\/li>\n<li class=\"c6 c15 li-bullet-0\"><span class=\"c3\">The value of $# is intact after the completion of the for loop.<\/span><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<h2>break<\/h2>\n<p>The break statement causes control to exit the current for, while, or until loop.\u00a0 One must be judicious in its use to avoid &#8220;spaghetti&#8221; code.\u00a0 Except in rare circumstances, adherence to structure programming principles leads to clearer and less error-prone code. In many cases the use of break should be avoided.<\/p>\n<table class=\"grid\" style=\"border-collapse: collapse;width: 100%;height: 45px\">\n<tbody>\n<tr style=\"height: 15px\">\n<td style=\"width: 50%;height: 15px\">\n<h4>Poor (and rampant) use of break<\/h4>\n<\/td>\n<td style=\"width: 50%;height: 15px\">\n<h4>Better code<\/h4>\n<\/td>\n<\/tr>\n<tr style=\"height: 15px\">\n<td style=\"width: 50%;height: 15px;vertical-align: top\">\n<pre>i=0\r\nwhile true\r\ndo\r\n  if [ $i -eq 5 ]\r\n  then\r\n    echo Reached 5\r\n    <strong>break<\/strong>\r\n  fi\r\n  i=$(( $i + 1 ))\r\ndone<\/pre>\n<\/td>\n<td style=\"width: 50%;height: 15px;text-align: left;vertical-align: top\">\n<pre>i=0\r\nwhile [ $i -ne 5 ]\r\ndo\r\n  i=$(( $i + 1 ))\r\ndone\r\necho Reached 5<\/pre>\n<\/td>\n<\/tr>\n<tr style=\"height: 15px\">\n<td style=\"width: 50%;height: 15px;vertical-align: top\">\n<h4>Observations<\/h4>\n<p>The use of an infinite loop (while true) is generally unnecessary (except perhaps in server processes) and should be avoided because it requires unstructured exit points leading to &#8220;spaghetti&#8221; code.\u00a0 Here, the break statement is used to jump out midway through the loop contributing to poor program structure.<\/p>\n<p>&nbsp;<\/td>\n<td style=\"width: 50%;height: 15px;vertical-align: top\">\n<h4>Observations<\/h4>\n<p>The purpose and limit of the loop is coded clearly in the while condition.\u00a0 Termination of the loop naturally follows coded instructions.\u00a0 There is no jumping out from the middle of the loop.\u00a0 The code is clearer, shorter, and better structured with a single entry and exit point for the loop.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"author":2,"menu_order":6,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":[],"pb_section_license":""},"chapter-type":[],"contributor":[],"license":[],"class_list":["post-41","chapter","type-chapter","status-publish","hentry"],"part":23,"_links":{"self":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/pressbooks\/v2\/chapters\/41","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/wp\/v2\/users\/2"}],"version-history":[{"count":7,"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/pressbooks\/v2\/chapters\/41\/revisions"}],"predecessor-version":[{"id":89,"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/pressbooks\/v2\/chapters\/41\/revisions\/89"}],"part":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/pressbooks\/v2\/parts\/23"}],"metadata":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/pressbooks\/v2\/chapters\/41\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/wp\/v2\/media?parent=41"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/pressbooks\/v2\/chapter-type?post=41"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/wp\/v2\/contributor?post=41"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/wp\/v2\/license?post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}