LAB ASSIGNMENT: Shell Scripting &
Variables
📌 Lab Objectives
By the end of this lab, you will be able to:
Create and run shell scripts
Use variables (string, numeric, environment, arrays)
Use user input and command substitution
Perform arithmetic operations
Work with special variables ($0, $1, $#, $?)
Validate input using conditionals
Build a complete shell script project
-------------------------------------
🧩 PART 1 — Working With Variables
-------------------------------------
🔹 Task 1: Create a Simple Script Using Variables
1. Create a file:
nano [Link]
2. Add the following content:
#!/bin/bash
name="Linux User"
city="Amman"
echo "Welcome $name"
echo "You are connecting from $city"
3. Make executable:
chmod +x [Link]
4. Run:
./[Link]
✔ Deliverable:
Screenshot of script + output.
🔹 Task 2: Command Substitution
Create a new script:
nano [Link]
Add:
#!/bin/bash
today=$(date)
uptime_now=$(uptime -p)
echo "Today is: $today"
echo "System uptime: $uptime_now"
Run it.
✔ Deliverable:
Screenshot of the script and output.
🔹 Task 3: User Input Variables
Create:
nano [Link]
Add:
#!/bin/bash
read -p "Enter your name: " user
read -p "Enter your age: " age
echo "Hello $user!"
echo "You are $age years old."
Run the script.
✔ Deliverable:
Screenshot of script + sample input.
-------------------------------------
🧩 PART 2 — Arithmetic & Special
Variables
-------------------------------------
🔹 Task 4: Perform Arithmetic
Create:
nano [Link]
Add:
#!/bin/bash
a=10
b=5
sum=$((a + b))
mul=$((a * b))
mod=$((a % b))
echo "Sum = $sum"
echo "Multiply = $mul"
echo "Remainder = $mod"
Run.
✔ Deliverable
Screenshot of results.
🔹 Task 5: Use Positional Variables ($1, $2, ...)
Create:
nano [Link]
Add:
#!/bin/bash
echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"
echo "Total arguments: $#"
Run with:
./[Link] Linux Bash
✔ Deliverable
Screenshot of command and output.
🔹 Task 6: Exit Status ($?)
Create:
nano [Link]
Add:
#!/bin/bash
ls /etc > /dev/null
echo "Exit code of last command: $? "
Run.
✔ Deliverable
Screenshot.
-------------------------------------
🧩 PART 3 — Arrays
-------------------------------------
🔹 Task 7: Array Creation and Looping
Create:
nano [Link]
Add:
#!/bin/bash
colors=("red" "green" "blue")
echo "First element: ${colors[0]}"
echo "All colors:"
for c in "${colors[@]}"; do
echo "$c"
done
Run.
✔ Deliverable
Screenshot of script and output.
-------------------------------------
🧩 PART 4 — Conditionals With Variables
-------------------------------------
🔹 Task 8: Validate User Input
Create:
nano [Link]
Add:
#!/bin/bash
read -p "Enter your age: " age
if [ -z "$age" ]; then
echo "Error: Age cannot be empty!"
exit 1
fi
if [ $age -lt 18 ]; then
echo "You are underage."
else
echo "Access granted."
fi
Run the script with valid and invalid input.
✔ Deliverable
Screenshot of multiple test runs
-------------------------------------
🧩 PART 5 — Final Script Project
-------------------------------------
🔥 Task 9 (Final Project): System Information Menu
Script
Create:
nano system_menu.sh
Add:
#!/bin/bash
while true; do
echo "-----------------------------------"
echo " Linux System Information Menu"
echo "-----------------------------------"
echo "1) Display Date and Time"
echo "2) Show Logged in Users"
echo "3) Show Disk Usage"
echo "4) Show System Uptime"
echo "5) Exit"
echo "-----------------------------------"
read -p "Choose an option: " opt
case $opt in
1) echo "Date: $(date)" ;;
2) who ;;
3) df -h ;;
4) uptime ;;
5) echo "Exiting..."; exit 0 ;;
*) echo "Invalid option!" ;;
esac
echo
done
Make executable and run:
chmod +x system_menu.sh
./system_menu.sh
✔ Deliverable
Screenshot showing at least 3 menu options being used.
summary :
1. Variables and Input/Output: 1 mastered
assigning variables, reading user input
(read), and accessing arguments
passed at runtime ($1, $2).
2. Arithmetic and Status: I learned to
perform calculations ($0) and use the
$? variable to reliably check the success
(0) or failure (non-zero) of the last
executed command.
3. Data Structures: I successfully created
and accessed basic data lists using
indexed arrays, which is key for
managing multiple related values.
4. Conditionals (I/Else): I implemented
decision-making logic using if/elif/else/fi
structures and comparison operators
(-gt,-It) to control script flow based on
conditions.
5. Loops (For/While): I applied both for and
while loops to automate repetitive tasks,
demonstrating control over iteration
and sequence within a script.
Submit a folder containing:
✔ Screenshots for each task
✔ All scripts (.sh files)
✔ A short summary (5–10 lines) of what you learned
Accepted formats:
ZIP
PDF + scripts