8.5. Lab: Modular Scripting#
8.5.1. Exercise 6: Backup Manager with Functions#
Create a comprehensive backup solution using modular functions.
File: lib_backup.sh
Create functions:
backup_init: Initialize backup directory, validate permissionsbackup_validate_source: Check that source exists and is readablebackup_create: Create tar.gz backup with timestampbackup_list: List available backupsbackup_restore: Restore from a backup with confirmationbackup_cleanup: Delete old backups (keep only N most recent)
Main Script: backup.sh
Usage:
$ ./backup.sh create /home/user/documents # Create backup
$ ./backup.sh list # List backups
$ ./backup.sh restore documents_20240119 # Restore specific backup
$ ./backup.sh cleanup 5 # Keep only 5 most recent
Requirements:
All functions use local variables
Clear error messages to stderr
Appropriate return codes for different failures
Confirmation prompts for destructive operations
Backup directory:
/backups(configurable)Naming format:
source_YYYYMMDD_HHMMSS.tar.gz
Hints:
Use timestamp functions for naming
Use
tar czffor compressionValidate prerequisites before operations
Use read prompts for confirmations (Exercise 4, Chapter 7)
8.5.2. Exercise 5: System Check Script#
Create a comprehensive system checking library.
File: lib_syscheck.sh
Create functions:
check_disk_space: Warn if usage > 80%check_memory_usage: Warn if usage > 90%check_running_services: List critical services status (ssh, httpd, etc.)check_network: Verify internet connectivitygenerate_report: Call all checks and produce a summary
Example Output:
=== System Health Report ===
Disk Usage: 65% (OK)
Memory Usage: 42% (OK)
Network: OK (google.com reachable)
Services:
sshd: running ✓
httpd: running ✓
mysql: running ✓
Overall Status: HEALTHY
Hints:
Use
dffor disk spaceUse
freefor memoryUse
systemctl statusfor servicesUse
pingfor network check (with timeout)Format output with colors or symbols for readability
8.5.3. Exercise 4: Configuration File Parser#
Create functions to manage and validate configuration.
Requirements:
Function
config_load: Load config from file (format:key=value)Function
config_get: Get value for a key, return error if not foundFunction
config_set: Update or add a key-value pairFunction
config_validate: Ensure required keys existCreate a config file and script that uses these functions
Use associative arrays if bash 4+, otherwise use namespaced variables
Example Config File (app.conf):
app_name=MyApp
app_version=1.0.0
app_port=8080
db_host=localhost
db_port=5432
Example Usage:
$ source lib_config.sh
$ config_load app.conf
$ config_get app_port # Output: 8080
$ config_set app_port 9000
Hints:
Parse config using
while IFS='=' readloopValidate required keys in
config_validateHandle missing/malformed config gracefully
8.5.4. Exercise 3: File Statistics Function#
Create a function that provides comprehensive file statistics.
Requirements:
Function
file_stats: Takes filename as argumentReturns: file size (bytes), line count, word count, last modified time
Return status 0 on success, 1 if file not found, 2 if not readable
Output format:
size:lines:words:modifiedHandle errors gracefully with messages to stderr
Example Run:
$ ./file_analyzer.sh /etc/passwd
"/etc/passwd" stats:
Size: 2156 bytes
Lines: 42
Words: 1847
Modified: 2024-01-15 14:23:45
Hints:
Use
statto get file size and modification timeUse
wcto get line and word countsCheck file existence and readability
Return appropriate codes for different error conditions
8.5.5. Exercise 2: String Utility Library#
Create a reusable library of string functions.
File: lib_string.sh
Create the following functions:
string_length: Return length of a stringstring_upper: Convert string to uppercasestring_lower: Convert string to lowercasestring_reverse: Reverse a stringstring_is_empty: Check if string is empty (return 0/1)string_contains: Check if one string contains another
Example Usage:
source lib_string.sh
word="Hello"
string_length "$word" # Output: 5
string_upper "$word" # Output: HELLO
string_reverse "$word" # Output: olleH
Hints:
Use
${#variable}for string lengthUse
trfor case conversionUse
revfor reversing (or loop with parameter expansion)Use
[[ $string =~ pattern ]]for substring checking
8.5.6. Exercise 1: Temperature Converter#
Create functions to convert between temperature scales.
Requirements:
Function
celsius_to_fahrenheit: Takes Celsius as argument, returns FahrenheitFunction
fahrenheit_to_celsius: Takes Fahrenheit as argument, returns CelsiusFunction
celsius_to_kelvin: Takes Celsius as argument, returns KelvinMain script that reads a temperature and scale, converts to all other scales
Use local variables in all functions
Handle invalid input with appropriate return codes
Example Run:
$ ./temp_converter.sh 0 C
0°C = 32°F, 273.15K
Hints:
Formulas: F = C × 9/5 + 32, K = C + 273.15
Use bash arithmetic with decimals carefully (consider using
awkfor decimal math)Validate input before processing