A comprehensive collection of bash scripts demonstrating fundamental to intermediate shell scripting concepts, from basic scripts to production-ready maintenance automation.
Scripts covering foundational bash concepts.
- Purpose: First shell script introduction
- Concepts: Shebang line, echo command, script execution
- Key Learning: Scripts require shebang (
#!/bin/bash) and executable permissions
- Purpose: Demonstrate variable usage and quoting differences
- Concepts: Variable assignment, single vs. double quotes, variable expansion
- Key Learning: Double quotes allow variable expansion; single quotes treat content literally
- Purpose: User input handling with the
readcommand - Concepts:
readcommand, user prompts, variable interpolation - Key Learning: Collect user input and use it in output messages
- Purpose: Conditional logic with if-else statements
- Concepts: Numeric comparisons (
-gt,-lt), if-elif-else structures - Key Learning: Evaluate numbers and branch logic based on conditions
- Purpose: File existence validation
- Concepts: File test operators (
-f), conditional execution - Key Learning: Check if files exist before processing them
- Purpose: Service status monitoring with user interaction
- Concepts: Variables, conditionals,
systemctlcommand integration - Key Learning: Combine user input with system commands for automation
Scripts demonstrating control flow and robust script design.
- Purpose: Iterate through arrays using for loops
- Concepts: Array definition, for-in loops, array expansion
- Key Learning: Loop through collections efficiently
- Purpose: Print numbers 1-10 using C-style for loops
- Concepts: C-style for loop syntax, numeric iteration
- Key Learning: Alternative loop syntax for numeric ranges
- Purpose: Countdown timer using while loops
- Concepts: While loops, variable arithmetic,
sleepcommand - Key Learning: While loops for condition-based iteration
- Purpose: Accept command-line arguments
- Concepts:
$1positional argument, argument validation, usage messages - Key Learning: Pass data to scripts via command line
- Purpose: Demonstrate all argument variables
- Concepts:
$#(argument count),$@(all arguments),$0(script name) - Key Learning: Access and use command-line arguments
- Purpose: Automated package installation with status checking
- Concepts: Loops, package manager integration (
apt-get), conditional installation - Key Learning: Automate system administration tasks
- Purpose: Error handling best practices
- Concepts:
set -e(exit on error),||operator for error handling - Key Learning: Make scripts fail safely and predictably
Scripts showcasing modular design and advanced techniques.
- Purpose: Basic function definition and calling
- Concepts: Function syntax, function calls, local variables
- Key Learning: Functions make scripts modular and reusable
- Purpose: System resource monitoring with functions
- Concepts: Functions with specific purposes,
dfandfreecommands, main function pattern - Key Learning: Organize scripts with dedicated functions for each task
- Purpose: Demonstrate strict mode options
- Concepts:
set -e,set -u,set -o pipefail - Key Learning: Use strict mode flags to catch errors early
- Purpose: Local vs. global variable scope
- Concepts:
localkeyword, variable scope, function isolation - Key Learning: Local variables prevent variable leakage between functions
- Purpose: Comprehensive system information reporter
- Concepts: Multiple functions, command substitution, output formatting
- Key Learning: Combine multiple functions for complex reporting tasks
Production-ready scripts for server maintenance automation.
- Purpose: Automated log file management
- Concepts:
findcommand,gzipcompression, file deletion, timestamps - Key Learning: Compress logs older than 7 days; delete
.gzfiles older than 30 days - Usage: `./log_rotate