🐧 Module 12 · Beginner Adventure

Shell Scripting Basics

Learn how to turn repeated Linux commands into tiny automation helpers. This reference mixes concepts, command labs, animated flows, terminal simulations, quizzes, and troubleshooting.

Start Mission Jump to Lab

Progress: 0% β€” complete activities to unlock badges.

check_disk.sh

1#!/bin/bash
2echo "Disk Report"
3df -h
4echo "Done"
Voice narration is off. Click β€œVoice On” or any πŸ”Š explanation button.

Voice Coach Mode πŸ”Š

Optional narration

Let the browser explain concepts while learners interact

Use the voice buttons when teaching live, or switch Voice On so the page explains what happened after animations, terminal steps, and quiz answers.

Note: most browsers require a learner click before audio starts. Voice quality depends on the browser and operating system voices.

Mission Brief: Teach the Server to Help Itself πŸ€–

Story mode

The server is bored of repeated manual commands. Your job is to become a Linux Automation Cadet and give the server small scripts it can run again and again.

Core idea: A shell script is simply a text file containing Linux commands, written in the order you want them to run.
  • Write commands in a file.
  • Add a shebang: #!/bin/bash.
  • Make the file executable using chmod +x.
  • Run it using ./script_name.sh.
Learning outcome

By the end, learners can read and create beginner shell scripts using variables, input, conditions, loops, and exit status.

🧠 Concepts πŸ§ͺ Lab πŸ† Quiz

Concept Cards: Shell Scripting Made Simple

Shell script

πŸ“œ Recipe for Linux

A script is like a recipe. Each line is one instruction for the shell to follow.

Example: backup filesExample: check disk
Variable

πŸ“¦ Storage box

A variable stores a value so the script can reuse it later.

name="JP"
echo "Hello $name"
echo

πŸ“’ Speaker

echo prints text or variable values on screen.

echo "Welcome to shell scripting"
read

πŸŽ™οΈ Microphone

read takes input from the user and stores it in a variable.

read -p "Enter name: " name
echo "Hello $name"
if condition

🚦 Decision gate

if lets a script choose what to do depending on a condition.

if [ "$user" = "root" ]; then
  echo "Admin user"
else
  echo "Normal user"
fi
for loop

πŸ” Repeat machine

for repeats commands for each item in a list.

for item in disk memory users
do
  echo "Checking $item"
done
Exit status

βœ… Result checker

$? stores whether the previous command succeeded. 0 usually means success. Non-zero usually means failure.

df -h
if [ $? -eq 0 ]; then
  echo "Success"
else
  echo "Failed"
fi
Basic automation

πŸ› οΈ Why scripting matters

Automation is useful when the same task is done many times: checking disk space, collecting logs, backing up files, validating services, or generating reports.

Manual workScripted work
Type commands every time.Run one script whenever needed.
Easy to forget steps.Same steps run consistently.
Hard to repeat for many servers.Can be reused and scheduled.

Animated Flow: What Happens When a Script Runs?

Click the button and watch the script travel from text file to command output.

1
Script file
check_disk.sh
β†’
2
Shebang
#!/bin/bash
β†’
3
Bash reads lines
β†’
4
df -h runs
β†’
5
Disk output appears

Command Playground: Predict, Run, Understand

Variables + echo

Try the name box

Enter a name and see how a variable is reused inside echo.

Output appears here.
if condition

Disk warning simulator

Set disk usage and let the script decide whether to warn you.

Try 45, 82, or 95.
for loop

Loop through services

Click to simulate a loop checking common Linux services.

Loop output appears here.
Exit status

Success or failure?

Choose a command result and watch $? decide the next message.

OK
FAIL
Exit status result appears here.

Interactive Terminal Simulator

This does not change your computer. It simulates the training lab so learners can understand the sequence before running it on Linux.

student@linux-lab:~
$ Ready. Click a mission command below.

Main Lab: Create Your First Disk Check Script πŸ§ͺ

Hands-on lab

Run these commands on Ubuntu, Rocky Linux, RHEL, or most Linux distributions with Bash available.

cat > check_disk.sh <<'EOF'
#!/bin/bash
df -h
EOF

chmod +x check_disk.sh
./check_disk.sh
Why ./check_disk.sh? The current directory is usually not searched automatically for commands. ./ means β€œrun the script from this directory.”
Expected output
output
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        40G   18G   22G  45% /
tmpfs           3.9G     0  3.9G   0% /dev/shm
...

The exact filesystem names and sizes will differ on every machine.

Script Evolution: From Tiny Script to Useful Automation

Beginners often understand single commands but struggle to combine them. This section shows the same script growing step-by-step.

Version 1: Basic disk check

#!/bin/bash
df -h

Version 2: Add a heading

#!/bin/bash
echo "Disk usage report:"
df -h

Version 3: Save report to a file

#!/bin/bash
df -h > disk_report.txt
echo "Report saved to disk_report.txt"

Version 4: Check exit status

#!/bin/bash
df -h > disk_report.txt

if [ $? -eq 0 ]; then
  echo "Disk report created successfully."
else
  echo "Disk report failed."
fi

Version 5: Ask user before running

#!/bin/bash

read -p "Do you want to check disk usage? yes/no: " answer

if [ "$answer" = "yes" ]; then
  df -h
else
  echo "Okay, skipping disk check."
fi

Common Beginner Errors and Fixes 🧯

Error / SymptomLikely CauseFix
Permission deniedScript is not executable.chmod +x check_disk.sh
command not foundWrong script path or missing ./../check_disk.sh
Variable does not expandUsed single quotes around variable.Use "Hello $name", not 'Hello $name'.
[: missing `]'Missing spaces inside test brackets.Use [ "$x" -gt 10 ].
Script uses wrong shellMissing or incorrect shebang.Add #!/bin/bash as the first line.

Boss Lab: Server Health Checker 🐲

This final challenge combines echo, commands, simple structure, and automation thinking.

cat > health_check.sh <<'EOF'
#!/bin/bash

echo "=============================="
echo " Simple Server Health Checker "
echo "=============================="

echo
echo "Disk Usage:"
df -h

echo
echo "Memory Usage:"
free -h

echo
echo "Logged-in Users:"
who

echo
echo "System Uptime:"
uptime
EOF

chmod +x health_check.sh
./health_check.sh
Trainer discussion
  • Which command checks disk usage?
  • Which command checks memory?
  • Which command shows logged-in users?
  • How can we save this report to a file?

Knowledge Check Quiz 🎯

1. What does #!/bin/bash do?
2. What does chmod +x script.sh do?
3. What does exit status 0 usually mean?
4. Which command accepts user input?
Score appears here.

Trainer Notes: Make the Session Fun and Sticky

Warm-up

Ask this first

β€œWhich Linux task do you repeat often?” Then show how a script can turn that repeated task into one file.

Live demo

Use mistakes deliberately

Run the script before chmod +x so learners see Permission denied, then fix it. This makes troubleshooting memorable.

Pair lab

Driver and navigator

One learner types, the other explains each command. Swap roles after the first script.

Extension

Take-home task

Ask learners to create my_report.sh that prints date, hostname, disk usage, memory usage, and uptime.