{"id":79080,"date":"2026-11-17T01:54:41","date_gmt":"2026-11-17T01:54:41","guid":{"rendered":"http:\/\/bangla.sitestree.com\/?p=79080"},"modified":"2026-11-17T01:54:41","modified_gmt":"2026-11-17T01:54:41","slug":"linux-intermediate-04-control","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=79080","title":{"rendered":"Linux Intermediate 04 \u2014 Bash Conditions, Loops, Functions, and Debugging"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/bangla.sitestree.com\/?name=linux-course-index\">Linux course index<\/a> \u00b7 <a href=\"https:\/\/bangla.sitestree.com\/?cat=1991\">Linux Intermediate category<\/a> \u00b7 Lesson 4 of 9<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>AI disclosure<\/strong> \u2014 AI-generated content, posted as is. AI can make mistakes. This lesson draws on the linked Open Education posts and adds original explanations or lab examples where useful. The newly written explanations and examples may be freely reused. Linked source material retains its own terms.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Learning goal<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use a case statement, a function, and a loop to generate a multiplication chart and test invalid input.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Concepts and setup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">if tests a command&#8217;s exit status. case matches a value against shell patterns. for iterates a sequence and while repeats while a condition succeeds. until repeats while its condition fails. Bash functions can use local variables, accept arguments, and return status. Write results to stdout and errors to stderr so another program can use the output.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The site has separate while- and for-loop versions of this multiplication chart. This version checks the argument once and reuses one function rather than copying the same logic into ten case branches.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Build a chart and check the failure path<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>lab=$(mktemp -d)\ncd &quot;$lab&quot; || exit\ncat &gt; chart.sh &lt;&lt;'EOF'\n#!\/usr\/bin\/env bash\nchart() {\n  local n=$1 i\n  for ((i=1; i&lt;=10; i++)); do\n    printf '%d * %d = %d\\n' &quot;$n&quot; &quot;$i&quot; &quot;$((n*i))&quot;\n  done\n}\ncase &quot;$1&quot; in\n  [1-9]|10) chart &quot;$1&quot; ;;\n  *) printf 'Usage: %s NUMBER (1-10)\\n' &quot;$0&quot; &gt;&amp;2; exit 2 ;;\nesac\nEOF\nbash -n chart.sh\nbash chart.sh 9 &gt; chart.txt\nhead -n 2 chart.txt\ntail -n 1 chart.txt\nbash chart.sh 11\nprintf 'status=%s\\n' &quot;$?&quot;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Possible output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>9 * 1 = 9\n9 * 2 = 18\n9 * 10 = 90\nUsage: chart.sh NUMBER (1-10)\nstatus=2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">bash -n checks syntax but does not execute the script. bash -x shows command expansions when diagnosing a lab script. Avoid tracing scripts that handle secrets.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Verify the loop boundary<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>wc -l &lt; chart.txt\ngrep -Fx '9 * 10 = 90' chart.txt\nbash -x chart.sh 2 &gt; two.txt 2&gt; trace.txt\ntail -n 1 two.txt<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Possible output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10\n9 * 10 = 90\n2 * 10 = 20<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Checking both the line count and the last value helps catch off-by-one errors.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Questions only<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Quick-check questions<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n\n<li>When does the wildcard case branch run?<\/li>\n\n\n<li>What does bash -n detect that it cannot prove about output?<\/li>\n\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Assessment questions<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n\n<li>Rewrite the for loop as a while loop.<\/li>\n\n\n<li>What should be tested for arguments 1, 10, and 0?<\/li>\n\n\n<li>How would you save compiler-like traces without mixing them with chart output?<\/li>\n\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Questions and answers<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Quick-check answers<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n\n<li><strong>When does the wildcard case branch run?<\/strong><br><strong>Answer<\/strong> \u2014 When none of the earlier patterns match, including when no argument is given.<\/li>\n\n\n<li><strong>What does bash -n detect that it cannot prove about output?<\/strong><br><strong>Answer<\/strong> \u2014 It detects syntax errors. It cannot prove the chart contains correct values.<\/li>\n\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Assessment answer key<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n\n<li><strong>Rewrite the for loop as a while loop.<\/strong><br><strong>Answer<\/strong> \u2014 Set local i=1, then use while ((i&lt;=10)); do, print the row, increment i with ((i++)), and end with done.<\/li>\n\n\n<li><strong>What should be tested for arguments 1, 10, and 0?<\/strong><br><strong>Answer<\/strong> \u2014 1 and 10 should succeed with 10 rows and correct endpoints. 0 should produce usage on stderr with status 2.<\/li>\n\n\n<li><strong>How would you save compiler-like traces without mixing them with chart output?<\/strong><br><strong>Answer<\/strong> \u2014 Run bash -x chart.sh 2 &gt; two.txt 2&gt; trace.txt in the lab.<\/li>\n\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Source articles on this site<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n\n<li><a href=\"https:\/\/bangla.sitestree.com\/?p=65716\">While Loop Example in Shell Programming. Display a Chart #Linux\/Unix: Shell Programming \u2013 001<\/a><\/li>\n\n\n<li><a href=\"https:\/\/bangla.sitestree.com\/?p=65710\">Another Example of Case and For Loop #Linux\/Unix: Shell Programming \u2013 001<\/a><\/li>\n\n\n<li><a href=\"https:\/\/bangla.sitestree.com\/?p=65695\">Use of For Loops and Functions in Shell Programming #Linux\/Unix: Shell Programming \u2013 001<\/a><\/li>\n\n\n<li><a href=\"https:\/\/bangla.sitestree.com\/?p=65730\">Topic 2: Example: Debug a Script #Linux\/Unix: Shell Programming \u2013 001<\/a><\/li>\n\n\n<li><a href=\"https:\/\/bangla.sitestree.com\/?p=76457\">Linux Shell Script: Until Example<\/a><\/li>\n\n\n<li><a href=\"https:\/\/bangla.sitestree.com\/?p=78657\">20 mixed quiz questions on Quoting and Control Statements.<\/a><\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Continue the course<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Previous lesson 03 \u2014 Bash Variables, Quoting, and Input. Next lesson 05 \u2014 Package Managers and Repositories.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/bangla.sitestree.com\/?name=linux-course-index\">Linux course index<\/a> \u00b7 <a href=\"https:\/\/bangla.sitestree.com\/?cat=1991\">Linux Intermediate category<\/a> \u00b7 Lesson 4 of 9<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Use a case statement, a function, and a loop to generate a multiplication chart and test invalid input.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[1991],"tags":[],"class_list":["post-79080","post","type-post","status-publish","format-standard","hentry","category-linux-intermediate","item-wrap"],"jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":79102,"url":"http:\/\/bangla.sitestree.com\/?p=79102","url_meta":{"origin":79080,"position":0},"title":"Linux Administration Course \u2014 Start Here and Lesson Index","author":"Sayed","date":"November 17, 2026","format":false,"excerpt":"A 33-lesson Linux course from shell fundamentals to server administration, performance, and a recovery capstone.","rel":"","context":"In &quot;Linux Basics&quot;","block_context":{"text":"Linux Basics","link":"http:\/\/bangla.sitestree.com\/?cat=1990"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":79079,"url":"http:\/\/bangla.sitestree.com\/?p=79079","url_meta":{"origin":79080,"position":1},"title":"Linux Intermediate 03 \u2014 Bash Variables, Quoting, and Input","author":"Sayed","date":"November 17, 2026","format":false,"excerpt":"Write Bash variables, capture a command result, and accept input without losing spaces or misinterpreting nonnumeric input.","rel":"","context":"In &quot;Linux Intermediate&quot;","block_context":{"text":"Linux Intermediate","link":"http:\/\/bangla.sitestree.com\/?cat=1991"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":79082,"url":"http:\/\/bangla.sitestree.com\/?p=79082","url_meta":{"origin":79080,"position":2},"title":"Linux Intermediate 06 \u2014 Boot, Services, Logs, and Timers","author":"Sayed","date":"November 17, 2026","format":false,"excerpt":"Trace the boot-to-service path, inspect logs, and create a timer that runs a tiny user service.","rel":"","context":"In &quot;Linux Intermediate&quot;","block_context":{"text":"Linux Intermediate","link":"http:\/\/bangla.sitestree.com\/?cat=1991"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":79078,"url":"http:\/\/bangla.sitestree.com\/?p=79078","url_meta":{"origin":79080,"position":3},"title":"Linux Intermediate 02 \u2014 Access Control Lists and Shared Directories","author":"Sayed","date":"November 17, 2026","format":false,"excerpt":"Grant and remove a narrowly scoped named-user ACL and distinguish a grant from effective access.","rel":"","context":"In &quot;Linux Intermediate&quot;","block_context":{"text":"Linux Intermediate","link":"http:\/\/bangla.sitestree.com\/?cat=1991"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":79084,"url":"http:\/\/bangla.sitestree.com\/?p=79084","url_meta":{"origin":79080,"position":4},"title":"Linux Intermediate 08 \u2014 SSH Keys, Host Identity, and File Transfer","author":"Sayed","date":"November 17, 2026","format":false,"excerpt":"Verify an SSH host\u2019s identity, use a passphrase-protected key, and transfer a non-sensitive file.","rel":"","context":"In &quot;Linux Intermediate&quot;","block_context":{"text":"Linux Intermediate","link":"http:\/\/bangla.sitestree.com\/?cat=1991"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":79085,"url":"http:\/\/bangla.sitestree.com\/?p=79085","url_meta":{"origin":79080,"position":5},"title":"Linux Intermediate 09 \u2014 Block Devices, Filesystems, and Mounts","author":"Sayed","date":"November 17, 2026","format":false,"excerpt":"Distinguish a block device from a filesystem and a mount point, and inspect each layer without changing it.","rel":"","context":"In &quot;Linux Intermediate&quot;","block_context":{"text":"Linux Intermediate","link":"http:\/\/bangla.sitestree.com\/?cat=1991"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/79080","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=79080"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/79080\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=79080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=79080"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=79080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}