Practice & Assessment
Test your understanding of Bash Scripting Fundamentals
Multiple Choice Questions
5What is wrong with this variable assignment: `MY_VAR = "hello"`?
What does `[[ -f "$FILE" ]]` test?
Inside a Bash function, why should you use `local` for variables?
What is the purpose of the shebang line `#!/bin/bash`?
What does the `$?` variable contain in Bash?
Coding Challenges
1Backup Script with Rotation
Write a Bash script backup.sh that accepts a source directory as $1 and a destination directory as $2. The script must: (1) validate both arguments are provided (exit 1 with message if not), (2) check the source directory exists with -d (exit 1 if not), (3) create the destination directory if it does not exist using mkdir -p, (4) create a timestamped tar.gz archive named backup_YYYYMMDD_HHMMSS.tar.gz in the destination, (5) delete backups older than 7 days in the destination using find with -mtime +7 -delete, (6) log each step with a timestamp using a log_info() function. Test with: ./backup.sh ~/projects ~/backups. Expected: archive created, old archives removed, all steps logged. Time estimate: 25 minutes.
Mini Project
System Health Check Script
Write a script health_check.sh that performs a full system health audit and generates a timestamped report. Requirements: (1) Check disk usage with df -h and warn if any partition is over 80% full (using awk to extract the percentage), (2) Check memory usage with free -m and report used vs total, (3) List the top 5 CPU-consuming processes using ps aux sorted by CPU, (4) Check if critical services (nginx, sshd, or any available service) are running using pgrep or systemctl, (5) Record script start and end time and compute duration, (6) Use functions for each check, local variables throughout, and write output to both stdout and ~/health_reports/report_TIMESTAMP.txt using tee. Exit 0 if all checks pass, exit 1 if any warning was triggered.
