# Shell Script Conditionals and Loops for Beginners **(Week 3 – Cohort 3 Bootcamp)** Shell scripting is a practical way to automate tasks and control system behavior in Linux. If you're new to it just like me, one of the first things you'll use are **conditionals** and **loops**. These control how and when your script runs certain commands. This article summarizes my Week 3 learning experience at the Web2 Bootcamp (Cohort 3). I worked with conditionals and loops, wrote functional scripts, and got comfortable using Bash for basic automation. ## CONDITIONALS Conditionals are used to make decisions in your script. They let the script choose actions based on whether a condition is true or false. ### 1. `if` Statement Used to test a condition. If the condition is true, the code block runs. ### 2. `if-else` Adds an alternative block that runs if the condition is false. ### 3. `elif` Short for "else if". Lets you test multiple conditions in sequence. ### Truth Table for Conditionals Condition Result | true | Executes `then` block | | -------- | -------- | | false | Skips `then`, checks next (`elif` or `else`) ## LOOPS Loops let your script repeat actions multiple times, either for a set number of items or while a condition remains true. ### 1. `for` Loop Used to loop over a list of values like numbers, filenames, or words. ### 2. `while` Loop Keeps running a set of commands as long as a condition stays true. ### What I feel was easy and can be easy for Beginners like Myself * Writing basic `if` and `else` logic * Looping through simple lists with `for` * Using numeric comparisons like `-eq`, `-gt`, `-lt` * Building readable and structured scripts ### What I feel Might Difficult to learn as a Beginner * Remembering to put spaces inside `[ ]` when writing conditions * Quoting variables properly to avoid unexpected results * Nesting loops and conditionals together * Handling invalid input or writing robust validation logic ## Common Comparison Operators for shell scripting | Operator | Description | |----------|--------------------| | `-eq` | Equal to | | `-ne` | Not equal to | | `-gt` | Greater than | | `-lt` | Less than | | `-ge` | Greater or equal | | `-le` | Less or equal | | `&&` | Logical AND | | `\|\|` | Logical OR | ## Examples I used for Practice ### Example 1: Grade Checker Script ![Screenshot from 2025-06-29 21-36-58](https://hackmd.io/_uploads/SyJe7VJHge.png) ![Screenshot from 2025-06-29 21-37-13](https://hackmd.io/_uploads/HygxX41Hxx.png) ![Screenshot from 2025-06-29 21-39-14](https://hackmd.io/_uploads/SJWx7VkHxx.png) ![Screenshot from 2025-06-29 21-40-00](https://hackmd.io/_uploads/SkeemNJHxe.png) ![Screenshot from 2025-06-29 21-40-32](https://hackmd.io/_uploads/SylgmNyBeg.png) ### Example 2: Car Engine Log Script ![Screenshot from 2025-06-29 21-30-38](https://hackmd.io/_uploads/S16c7VJSlx.png) ![Screenshot from 2025-06-29 21-31-01](https://hackmd.io/_uploads/HkAcXEkHll.png) ![Screenshot from 2025-06-29 21-31-18](https://hackmd.io/_uploads/SkC57Nkree.png) ![Screenshot from 2025-06-29 21-31-31](https://hackmd.io/_uploads/SJ0qm4JSgg.png) ![Screenshot from 2025-06-29 21-33-20](https://hackmd.io/_uploads/rk09XVJreg.png) ![Screenshot from 2025-06-29 21-34-30](https://hackmd.io/_uploads/HyR97E1Bxg.png) ### Example 3: Password Verification Script ![Screenshot from 2025-06-29 21-50-47](https://hackmd.io/_uploads/B1EmNVyrxl.png) ![Screenshot from 2025-06-29 21-50-54](https://hackmd.io/_uploads/ByIQNVJBlg.png) This week, I practiced how to control script flow using conditionals and loops. These examples helped add up my knowlede and skill on logic building and gave me real insights into how Bash scripting works in practical scenarios. I'm so excited about learning shell scripting. It can only get better.