Skip to content

In this lab, I learned how to use while loops, conditionals, and interactive menu-driven programs in Bash. The tasks focused on waiting for rocket states, fixing loop syntax errors, and building fully functional scripts that interact with the user in real time.

Notifications You must be signed in to change notification settings

1suleyman/-Linux-Shell-Script-Lab-While-Loops-Status-Checks-Menu-Driven-Programs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 

Repository files navigation

πŸ” Linux Shell Script Lab – While Loops, Status Checks & Menu-Driven Programs

In this lab, I learned how to use while loops, conditionals, and interactive menu-driven programs in Bash. The tasks focused on waiting for rocket states, fixing loop syntax errors, and building fully functional scripts that interact with the user in real time.


πŸ“‹ Lab Overview

Goal:

  • Use while loops to wait for changing system states
  • Fix scripts with missing loop syntax
  • Understand conditional branching inside loops
  • Build a menu-driven CLI calculator using infinite loops + user input

Learning Outcomes:

  • Use while [ condition ] loops effectively
  • Understand when to use sleep, state checks, and repeated polling
  • Debug syntactical errors inside loop structures
  • Apply interactive read statements inside loops
  • Build reusable automation scripts that loop until user exit conditions

πŸ›  Step-by-Step Journey


Step 1 β€” Add a While Loop to Wait for Rocket Launch State

Script path:

/home/bob/create-and-launch-rocket

Goal: Add a loop that waits while the rocket is in the "launching" state before running the failure debug tool.

Final Script Snippet:

mission_name=$1

mkdir $mission_name
rocket-add $mission_name
rocket-start-power $mission_name
rocket-internal-power $mission_name
rocket-start-sequence $mission_name
rocket-start-engine $mission_name
rocket-lift-off $mission_name

rocket_status=$(rocket-status $mission_name)
echo "The status of launch is $rocket_status"

while [ $rocket_status = "launching" ]
do
  sleep 2
  rocket_status=$(rocket-status $mission_name)
done

if [ $rocket_status = "failed" ]
then
  rocket-debug
fi

βœ… Script now waits until the rocket stops launching βœ… If FAILED β†’ runs rocket-debug βœ… If SUCCESS β†’ finishes normally


Step 2 β€” Fix Syntax Error in print-numbers.sh

Script path:

/home/bob/print-numbers.sh

Original (broken):

to_number=$1
number=0
while [ $number -lt $to_number ]
  echo $(( number++ ))
done

Issue: Missing the do keyword.

Fixed:

to_number=$1
number=0

while [ $number -lt $to_number ]
do
  echo $(( number++ ))
done

After running:

bash print-numbers.sh 10

Output:

0
1
2
3
4
5
6
7
8
9

Step 3 β€” Identify What the Script Does

Answer: βœ” It prints numbers from 0 up to (but not including) the input value.


Step 4 β€” Build a Menu-Driven Calculator

Script path:

/home/bob/calculator.sh

Requirements: βœ” Show a menu: Add, Subtract, Multiply, Divide, Quit βœ” Accept two numbers when needed βœ” Loop until user chooses option 5 βœ” Print results in the format: Answer=6

Final Script:

while true
do
  echo "1. Add"
  echo "2. Subtract"
  echo "3. Multiply"
  echo "4. Divide"
  echo "5. Quit"

  read -p "Enter your choice: " choice

  if [ $choice -eq 1 ]
  then
        read -p "Enter Number1: " number1
        read -p "Enter Number2: " number2
        echo Answer=$(( number1 + number2 ))

  elif [ $choice -eq 2 ]
  then
        read -p "Enter Number1: " number1
        read -p "Enter Number2: " number2
        echo Answer=$(( number1 - number2 ))

  elif [ $choice -eq 3 ]
  then
        read -p "Enter Number1: " number1
        read -p "Enter Number2: " number2
        echo Answer=$(( number1 * number2 ))

  elif [ $choice -eq 4 ]
  then
        read -p "Enter Number1: " number1
        read -p "Enter Number2: " number2
        echo Answer=$(( number1 / number2 ))

  elif [ $choice -eq 5 ]
  then
        break
  fi

done

Make executable:

chmod +x calculator.sh

βœ” Menu repeats indefinitely βœ” Input-driven βœ” Fully interactive βœ” Ends only when user selects option 5


🧠 Key Concepts Reinforced

Concept Description
While loop Repeats while a condition remains true
Polling status Used for waiting on processes (e.g., rocket-state)
Menu-driven programs Built using infinite loops (while true)
User input read accepts numbers/choices interactively
Conditional logic if / elif / else to handle branching paths
Debugging syntax errors Ensuring every while has a matching do

🧩 Troubleshooting Patterns Learned

Issue Fix
Missing do in while loop Add do before loop body
Incorrect variable comparison Use [ $var = "value" ]
Infinite loop without exit Add break on quit condition
Wrong command order Ensure checks come after updated state

πŸ’‘ Notes / Tips

  • Loops are the backbone of automation β€” polling and planning tasks rely heavily on them.

  • Always remember the structure:

    while [ condition ]
    do
       commands
    done
    
  • When building interactive scripts, always loop until the user chooses to exit.

  • Use sleep inside loops to avoid high CPU usage.

  • Keep menus clean and user-friendly β€” they form the UI of shell scripts.


βœ… Summary Commands

Task Command
Edit script vi scriptname.sh
Save & exit :wq
Make executable chmod +x scriptname.sh
Run script bash scriptname.sh
Test interactive script Run and follow prompts
Loop through states while [ condition ]; do ... done

🏁 End of Lab

Successfully completed multiple loop-based scripting tasks: βœ… Added a rocket state polling loop βœ… Fixed syntactical loop errors βœ… Built an interactive menu-driven calculator βœ… Strengthened while-loop, state checking, and conditional branching skills

This lab deepened understanding of loops, interactive scripts, and continuous automation logic β€” foundational for real DevOps scripting.

About

In this lab, I learned how to use while loops, conditionals, and interactive menu-driven programs in Bash. The tasks focused on waiting for rocket states, fixing loop syntax errors, and building fully functional scripts that interact with the user in real time.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published