{"id":37,"date":"2018-10-20T08:00:00","date_gmt":"2018-10-20T00:00:00","guid":{"rendered":""},"modified":"2018-11-11T16:10:58","modified_gmt":"2018-11-11T08:10:58","slug":"the-linux-command-line-flow-control-part-2","status":"publish","type":"post","link":"https:\/\/yeslq.com\/?p=37","title":{"rendered":"The Linux Command Line&#8212;Flow Control &#8211; Part 2"},"content":{"rendered":"<h1 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\"><i style=\"font-size: x-small;\"><a href=\"http:\/\/linuxcommand.org\/tlcl.php\" style=\"color: #002740;\">The Linux Command Line&nbsp;<\/a><\/i><span style=\"font-size: xx-small;\">by William Shotts<\/span><\/h1>\n<div><span style=\"font-size: xx-small;\"><br \/><\/span><\/div>\n<div><span style=\"font-size: xx-small;\"><\/p>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Hold on to your hats. This lesson is going to be a big one!<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">More Branching<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">In the&nbsp;<a href=\"http:\/\/linuxcommand.org\/lc3_wss0080.php\" style=\"color: #002740;\">previous lesson on flow control<\/a>&nbsp;we learned about the&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">if<\/tt>&nbsp;command and how it is used to alter program flow based on a command&#8217;s exit status. In programming terms, this type of program flow is called&nbsp;<i>branching<\/i>&nbsp;because it is like traversing a tree. You come to a fork in the tree and the evaluation of a condition determines which branch you take.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">There is a second and more complex kind of branching called a&nbsp;<i>case<\/i>. A case is multiple-choice branch. Unlike the simple branch, where you take one of two possible paths, a case supports several possible outcomes based on the evaluation of a value.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">You can construct this type of branch with multiple&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">if<\/tt>&nbsp;statements. In the example below, we evaluate some input from the user:<\/div>\n<div style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">#!\/bin\/bash<br \/><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> -n \"Enter a number between 1 and 3 inclusive &gt; \"<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">read<\/tt> character<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">if<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">[<\/tt> \"$character\" = \"1\" <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">];<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">then<\/tt><br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"You entered one.\"<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">elif<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">[<\/tt> \"$character\" = \"2\" <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">];<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">then<\/tt><br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"You entered two.\"<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">elif<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">[<\/tt> \"$character\" = \"3\" <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">];<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">then<\/tt><br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"You entered three.\"<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">else<\/tt><br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"You did not enter a number between 1 and 3.\"<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">fi<\/tt><br \/><\/tt><br \/><\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Not very pretty.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Fortunately, the shell provides a more elegant solution to this problem. It provides a built-in command called&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/caseh.html\" style=\"color: #002740;\">case<\/a><\/tt>, which can be used to construct an equivalent program:<\/div>\n<div style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">#!\/bin\/bash<br \/><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> -n \"Enter a number between 1 and 3 inclusive &gt; \"<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">read<\/tt> character<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">case<\/tt> $character <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">in<\/tt><br \/>    1 ) <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"You entered one.\"<br \/>        ;;<br \/>    2 ) <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"You entered two.\"<br \/>        ;;<br \/>    3 ) <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"You entered three.\"<br \/>        ;;<br \/>    * ) <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"You did not enter a number between 1 and 3.\"<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">esac<\/tt><br \/>       <\/tt><br \/><\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">The&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">case<\/tt>&nbsp;command has the following form:<\/div>\n<div style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">case<\/tt> word <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">in<\/tt><br \/>    patterns <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">)<\/tt> commands <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">;;<\/tt><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">esac<\/tt><br \/>       <\/tt><br \/><\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\"><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">case<\/tt>&nbsp;selectively executes statements if word matches a pattern. You can have any number of patterns and statements. Patterns can be literal text or wildcards. You can have multiple patterns separated by the &#8220;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">|<\/tt>&#8221; character. Here is a more advanced example to show what I mean:<\/div>\n<div style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">#!\/bin\/bash<br \/><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> -n \"Type a digit or a letter &gt; \"<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">read<\/tt> character<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">case<\/tt> $character <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">in<\/tt><br \/>                                # Check for letters<br \/>    [[:lower:]] | [[:upper:]] ) <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"You typed the letter $character\"<br \/>                                ;;<br \/><br \/>                                # Check for digits<br \/>    [0-9] )                     <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"You typed the digit $character\"<br \/>                                ;;<br \/><br \/>                                # Check for anything else<br \/>    * )                         <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"You did not type a letter or a digit\"<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">esac<\/tt><br \/><\/tt><br \/><\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Notice the special pattern &#8220;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">*<\/tt>&#8220;. This pattern will match anything, so it is used to catch cases that did not match previous patterns. Inclusion of this pattern at the end is wise, as it can be used to detect invalid input.<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">Loops<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">The final type of program flow control we will discuss is called&nbsp;<i>looping<\/i>. Looping is repeatedly executing a section of your program based on the exit status of a command. The shell provides three commands for looping:&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">while<\/tt>,&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">until<\/tt>&nbsp;and&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">for<\/tt>. We are going to cover&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">while<\/tt>&nbsp;and&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">until<\/tt>&nbsp;in this lesson and&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">for<\/tt>&nbsp;in a upcoming lesson.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">The&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">while<\/tt>&nbsp;command causes a block of code to be executed over and over, as long as the exit status of a specified command is true. Here is a simple example of a program that counts from zero to nine:<\/div>\n<div style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">#!\/bin\/bash<br \/><br \/>number=0<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">while<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">[<\/tt> \"$number\" -lt 10 <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">];<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">do<\/tt><br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"Number = $number\"<br \/>    number=$((number + 1))<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">done<\/tt><br \/>       <\/tt><br \/><\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">On line 3, we create a variable called&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">number<\/tt>&nbsp;and initialize its value to 0. Next, we start the&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">while<\/tt>&nbsp;loop. As you can see, we have specified a command that tests the value of&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">number<\/tt>. In our example, we test to see if&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">number<\/tt>&nbsp;has a value less than 10.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Notice the word&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">do<\/tt>&nbsp;on line 4 and the word&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">done<\/tt>&nbsp;on line 7. These enclose the block of code that will be repeated as long as the exit status remains zero.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">In most cases, the block of code that repeats must do something that will eventually change the exit status, otherwise you will have what is called an&nbsp;<i>endless loop<\/i>; that is, a loop that never ends.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">In the example, the repeating block of code outputs the value of&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">number<\/tt>&nbsp;(the&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt>&nbsp;command on line 5) and increments&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">number<\/tt>&nbsp;by one on line 6. Each time the block of code is completed, the test command&#8217;s exit status is evaluated again. After the tenth iteration of the loop,&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">number<\/tt>&nbsp;has been incremented ten times and the&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">test<\/tt>&nbsp;command will terminate with a non-zero exit status. At that point, the program flow resumes with the statement following the word&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">done<\/tt>. Since<tt style=\"font-family: courier, lucidatypewriter, monospace;\">done<\/tt>&nbsp;is the last line of our example, the program ends.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">The&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">until<\/tt>&nbsp;command works exactly the same way, except the block of code is repeated as long as the specified command&#8217;s exit status is false. In the example below, notice how the expression given to the&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">test<\/tt>&nbsp;command has been changed from the&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">while<\/tt>&nbsp;example to achieve the same result:<\/div>\n<div style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">#!\/bin\/bash<br \/><br \/>number=0<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">until<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">[<\/tt> \"$number\" -ge 10 <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">];<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">do<\/tt><br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"Number = $number\"<br \/>    number=$((number + 1))<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">done<\/tt><br \/>       <\/tt><br \/><\/pre>\n<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">Building A Menu<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">One common way of presenting a user interface for a text based program is by using a&nbsp;<i>menu<\/i>. A menu is a list of choices from which the user can pick.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">In the example below, we use our new knowledge of loops and cases to build a simple menu driven application:<\/div>\n<div style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">#!\/bin\/bash<br \/><br \/>selection=<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">until<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">[<\/tt> \"$selection\" = \"0\" <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">];<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">do<\/tt><br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"<br \/>    PROGRAM MENU<br \/>    1 - Display free disk space<br \/>    2 - Display free memory<br \/><br \/>    0 - exit program<br \/>\"<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> -n \"Enter selection: \"<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">read<\/tt> selection<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"\"<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">case<\/tt> $selection <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">in<\/tt><br \/>        1 ) df ;;<br \/>        2 ) free ;;<br \/>        0 ) <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">exit<\/tt> ;;<br \/>        * ) <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"Please enter 1, 2, or 0\"<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">esac<\/tt><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">done<\/tt><br \/>       <\/tt><br \/><\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">The purpose of the&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">until<\/tt>&nbsp;loop in this program is to re-display the menu each time a selection has been completed. The loop will continue until selection is equal to &#8220;0,&#8221; the &#8220;exit&#8221; choice. Notice how we defend against entries from the user that are not valid choices.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">To make this program better looking when it runs, we can enhance it by adding a function that asks the user to press the Enter key after each selection has been completed, and clears the screen before the menu is displayed again. Here is the enhanced example:<\/div>\n<div style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">#!\/bin\/bash<br \/><br \/>press_enter()<br \/>{<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> -en \"nPress Enter to continue\"<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">read<\/tt><br \/>    clear<br \/>}<br \/><br \/>selection=<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">until<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">[<\/tt> \"$selection\" = \"0\" <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">];<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">do<\/tt><br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"<br \/>    PROGRAM MENU<br \/>    1 - display free disk space<br \/>    2 - display free memory<br \/><br \/>    0 - exit program<br \/>\"<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> -n \"Enter selection: \"<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">read<\/tt> selection<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"\"<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">case<\/tt> $selection <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">in<\/tt><br \/>        1 ) df <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">;<\/tt> press_enter ;;<br \/>        2 ) free <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">;<\/tt> press_enter ;;<br \/>        0 ) <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">exit<\/tt> ;;<br \/>        * ) <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"Please enter 1, 2, or 0\"; press_enter<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">esac<\/tt><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">done<\/tt><br \/><\/tt><br \/><\/pre>\n<\/div>\n<p><br style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\" \/><\/p>\n<div style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 1em;\">\n<h2 style=\"font-weight: normal;\">When your computer hangs&#8230;<\/h2>\n<p>We have all had the experience of an application&nbsp;<i>hanging<\/i>. Hanging is when a program suddenly seems to stop and become unresponsive. While you might think that the program has stopped, in most cases, the program is still running but its program logic is stuck in an endless loop.<br \/>Imagine this situation: you have an external device attached to your computer, such as a USB disk drive but you forgot to turn it on. You try and use the device but the application hangs instead. When this happens, you could picture the following dialog going on between the application and the interface for the device:<\/p>\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">Application:    Are you ready?<br \/>Interface:  Device not ready.<br \/><br \/>Application:    Are you ready?<br \/>Interface:  Device not ready.<br \/><br \/>Application:    Are you ready?<br \/>Interface:  Device not ready.<br \/><br \/>Application:    Are you ready?<br \/>Interface:  Device not ready.<br \/><\/tt><br \/><\/pre>\n<p>and so on, forever.<br \/>Well-written software tries to avoid this situation by instituting a&nbsp;<i>timeout<\/i>. This means that the loop is also counting the number of attempts or calculating the amount of time it has waited for something to happen. If the number of tries or the amount of time allowed is exceeded, the loop exits and the program generates an error and exits.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; padding: 1em;\"><\/div>\n<p><\/span><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The Linux Command Line&nbsp;by William Shotts Hold on t &#8230; <a title=\"The Linux Command Line&#8212;Flow Control &#8211; Part 2\" class=\"read-more\" href=\"https:\/\/yeslq.com\/?p=37\" aria-label=\"\u9605\u8bfb The Linux Command Line&#8212;Flow Control &#8211; Part 2\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4,6],"tags":[],"class_list":["post-37","post","type-post","status-publish","format-standard","hentry","category-linux","category-shell","category-the-linux-command-line"],"_links":{"self":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/37","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=37"}],"version-history":[{"count":1,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/37\/revisions"}],"predecessor-version":[{"id":98,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/37\/revisions\/98"}],"wp:attachment":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}