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.
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
readstatements inside loops - Build reusable automation scripts that loop until user exit conditions
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
Script path:
/home/bob/print-numbers.sh
Original (broken):
to_number=$1
number=0
while [ $number -lt $to_number ]
echo $(( number++ ))
doneIssue:
Missing the do keyword.
Fixed:
to_number=$1
number=0
while [ $number -lt $to_number ]
do
echo $(( number++ ))
doneAfter running:
bash print-numbers.sh 10Output:
0
1
2
3
4
5
6
7
8
9
Answer: β It prints numbers from 0 up to (but not including) the input value.
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
doneMake executable:
chmod +x calculator.shβ Menu repeats indefinitely β Input-driven β Fully interactive β Ends only when user selects option 5
| 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 |
| 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 |
-
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
sleepinside loops to avoid high CPU usage. -
Keep menus clean and user-friendly β they form the UI of shell scripts.
| 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 |
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.