Shell Scripts for SRE and DevOps¶
Shell scripts are programs written for command-line interpreters (shells) that automate system tasks and operations. They combine sequences of commands into reusable scripts that can be executed as single units.
They are widely used by SRE (Site Reliability Engineers) and DevOps engineers to automate operations, reduce manual effort, and standardize workflows.
Historical Context and Evolution¶
Shell scripting originated in the early Unix systems (1970s) with the Bourne shell (sh). Over time, more feature-rich shells emerged:
- Bash (Bourne Again Shell): Default on most Linux distributions
- Zsh: Extended features with better user interaction
- Ksh: AIX default shell with advanced scripting capabilities
Why Shell Scripts Are Important for SRE and DevOps¶
Tip
Shell scripts remain a core tool for SRE and DevOps. - They are simple: use plain Linux commands. - They are powerful: integrate system utilities, APIs, and services. - They are universal: available by default on nearly every Unix/Linux system. - They allow conditions, loops, functions, and modularity to make tasks reusable.
Examples of usage:
- Daily health checks
- Disk and memory monitoring
- Service management
- Deployment pipelines
- Cron-based automation
Shell Interpreter and Location¶
- The interpreter is the program that executes shell scripts.
- Common locations:
- Default interpreter:
/bin/bashin most Linux systems. - Speed: Shell scripts are slower than compiled languages (C, Go), but fast enough for automation.
Note
Always define interpreter at the top of scripts with shebang:
bash #!/bin/bash
Core Components of Shell Scripts¶
Shebang Directive¶
Comments and Documentation¶
Variables and Data Types¶
NAME="value" # String
COUNT=42 # Integer
FILES=(*.txt) # Array
readonly CONST=100 # Constant
export GLOBAL_VAR # Env variable
Special Variables¶
$0 # Script name
$1 # First argument
$# # Number of args
$@ # All arguments
$? # Exit status
$$ # PID
$! # Background PID
How to Write Shell Scripts¶
1. Conditions¶
2. Loops¶
3. Functions¶
4. Case (Switch)¶
case $1 in
start) echo "Starting service" ;;
stop) echo "Stopping service" ;;
*) echo "Usage: $0 {start|stop}" ;;
esac
5. Modular Scripts¶
Advanced Scripting Techniques¶
Parameter Expansion¶
${VAR:-default} # Use default if unset
${VAR:=default} # Set default if unset
${#VAR} # Length
${VAR#pattern} # Remove prefix
${VAR%pattern} # Remove suffix
Arrays and Associative Arrays¶
Input/Output Handling¶
Error Handling and Debugging¶
Benefits of Shell Scripting¶
- Fast prototyping of automation.
- Integration with system tools (
systemctl,docker,kubectl,rsync). - Portability across Linux distributions.
- Low dependency: no need for extra runtimes.
Advanced Examples for Production Environments¶
1. Comprehensive System Health Check¶
(Full script with disk and memory checks, logging, colors.)
2. Advanced Log Analyzer¶
(Grep-based log analysis with report generation.)
3. Kubernetes Deployment Helper¶
(Validates YAML, applies deployment, waits for rollout.)
4. Secure Configuration Manager¶
(Encrypt/decrypt configs with OpenSSL.)
5. Advanced Backup System¶
(Database backups, retention, verification.)
Best Practices for Production Scripts¶
Security¶
- Avoid command injection.
- Use
mktempfor temp files.
Performance¶
- Prefer shell built-ins over external commands.
- Process large files with
while read.
Portability¶
- Prefer POSIX-compliant syntax when possible.
Documentation¶
- Include script headers (purpose, author, usage).
- Document functions with parameters and return codes.
Integration with Modern DevOps Tools¶
CI/CD Pipelines¶
(Shell stages for Jenkins, rollback, Slack alerts.)
Cloud Integration¶
- AWS:
aws s3 cp - GCP:
gsutil cp - Azure:
az storage blob upload
Monitoring and Logging¶
Performance Monitoring¶
- Track script execution time with
$SECONDS. - Send metrics to monitoring systems.
Structured Logging¶
- JSON logging with
jqfor easy integration.