Shell Scripting Projects
The best way to master shell scripting is by building real-world projects. Here is a list of practical script ideas ranging from beginner to advanced. Use these to practice conditionals, loops, APIs, and file parsing.
1. System Health Monitor
Goal: Create a script that checks critical system metrics and alerts you if they exceed a threshold.
Key Features:
- Use
df -hto check disk space. - Use
free -mto check RAM usage. - Use
uptimeortopto check CPU load. - If disk space is over 90%, use
loggeror append to a log file. - Advanced: Use
curlto send a webhook alert to Discord or Slack.
2. Bulk File Renamer
Goal: A script that renames multiple files in a directory based on user input.
Key Features:
- Accept command-line arguments for the directory, a prefix, and a file extension.
- Use a
forloop to iterate through the files. - Use string manipulation to rename files (e.g., changing
image1.jpgtoholiday_image1.jpg). - Advanced: Implement a "dry run" mode (using a flag parsed by
getopts) that only prints what would be renamed without actually modifying the files.
3. Automated User Manager
Goal: A script for system administrators to quickly create, modify, or delete Linux users.
Key Features:
- Use a
casestatement to provide a menu (1. Add User, 2. Delete User, 3. Change Password). - Ask for user input using
read. - Use
useraddanduserdelcommands. - Ensure the script checks if it's running as root before executing (check if
EUIDis 0).
4. API Weather Fetcher
Goal: Create a script that fetches the current weather for a specified city using a public API.
Key Features:
- Use
curlorwgetto make an HTTP GET request to a service likewttr.in. - Example command:
curl wttr.in/London?format=3 - Accept the city name as a command-line argument.
- Use
jq(if installed) to parse JSON responses from more complex weather APIs.
5. Log Analyzer
Goal: A script that parses web server logs (like Nginx or Apache) to find suspicious activity.
Key Features:
- Read an
access.logfile line by line or using tools likeawkandgrep. - Count the number of 404 (Not Found) errors.
- Identify the top 5 IP addresses that made requests.
- Advanced: Generate an HTML or Markdown report summarising the findings.
Structuring Larger Projects
When you move beyond single-file scripts, consider this directory structure for your projects:
my_project/
|-- bin/ # The executable script itself
|-- conf/ # Configuration files (.conf or .env)
|-- lib/ # Helper scripts/functions to be sourced
|-- logs/ # Directory for log files
|-- README.md # Documentation
You can import functions from lib/ into your main script using the source command:
source ./lib/utils.sh