14.5. Lab: Configuration & Data Structures#
14.5.1. Lab 6: Advanced Data Structure Implementation#
Implement complex data structures using arrays and demonstrate their application in real problems.
Requirements:
Implement stack data structure (LIFO)
Implement queue data structure (FIFO)
Implement linked list operations
Create hash table using associative arrays
Implement tree representation using arrays
Provide clear API for each structure
Demonstrate practical uses
Data Structures to Implement:
Stack:
push(value) - Add element to top
pop() - Remove and return top element
peek() - View top without removing
is_empty() - Check if stack empty
size() - Return number of elements
Queue:
enqueue(value) - Add to end
dequeue() - Remove from front
is_empty() - Check if empty
size() - Return number of elements
Hash Table (using associative array):
insert(key, value)
search(key)
delete(key)
contains(key)
Practical Applications:
Use stack: Expression evaluation, undo/redo
Use queue: Task scheduling, print queue
Use hash table: Caching, lookup tables
Use tree: File system representation, hierarchy
Testing Scenarios:
Stack: Push 1-5, pop all, verify order is reversed
Queue: Enqueue 1-5, dequeue all, verify order is same
Hash: Insert, search, delete, update values
Tree: Create hierarchy, traverse, find nodes
Validation:
All operations work correctly
Edge cases handled (empty, single element)
Performance is reasonable for bash
Applications demonstrate practical use
Code is well-documented
Bonus:
Implement binary search tree
Add sorting algorithms on arrays
Implement graph structure
Add performance benchmarks
Create visualization of structures
14.5.2. Lab 5: Customized Shell Environment Setup#
Build a complete shell environment configuration with aliases, functions, and customizations.
Requirements:
Create modular shell configuration files
Implement useful aliases for common tasks
Create helper functions for productivity
Customize prompt with useful information
Set up safe defaults (rm -i, etc.)
Support project-specific configurations
Optimize shell startup performance
Configuration Modules:
Base environment variables
System-specific aliases
Development tools (git, docker, etc.)
Safety aliases (protected rm, cp, etc.)
Navigation shortcuts
Project management functions
Aliases to Implement:
Navigation: cd, pushd, popd shortcuts
File operations: ls variants, find shortcuts
System: mem, disk, ports, processes
Development: git shortcuts, docker shortcuts
Safety: rm, cp, mv with confirmation
Search: grep, find with useful defaults
Functions to Create:
mkcd(dir) - Make and change to directory
project_init(name) - Initialize new project
backup_dir(src) - Quick directory backup
monitor_process() - Watch process performance
find_largest_files()- Find space hogs
Testing Requirements:
All aliases work correctly
All functions execute successfully
Prompt displays relevant information
Startup time is reasonable (<1 second)
Environment variables are set correctly
Custom Prompt Example:
[user@host] ~/project (main) $
Shows: user, hostname, current directory, git branch if in repo
Validation:
Aliases available in interactive shells
Functions execute with arguments
Prompt customization works
No startup errors
Can navigate and use file operations easily
Bonus:
Add git integration (branch in prompt)
Add Python virtualenv detection
Add docker container detection
Add battery/power status display
Create theme system for prompt
14.5.3. Lab 4: Shell Options and Debugging Configuration#
Create a debugging and configuration system that controls shell behavior based on environment settings.
Requirements:
Create configuration that sets appropriate shell options
Implement safe defaults (set -euo pipefail, etc.)
Create debug mode that shows execution details
Log all errors and warnings
Implement different behavior for dev/prod environments
Handle errors gracefully
Provide debugging output on demand
Configuration Levels:
Development: set -x (verbose), all options enabled
Testing: set -euo pipefail, some debug output
Production: silent, error handling only
Debug: full trace, break on warnings
Features:
Environment variable to control mode
Logging of all operations
Error handler with context
Warning system
Performance metrics (optional)
Test Scenario:
export ENV_MODE="dev"
./script.sh # Shows detailed execution
export ENV_MODE="prod"
./script.sh # Silent execution, errors logged
export ENV_MODE="debug"
./script.sh # Full trace with debugging
Expected Output (Dev Mode):
+ source config.sh
+ set_options
++ shopt -s nullglob
+ process_files
+ [[ -f file1.txt ]]
+ grep pattern file1.txt
[Results]
Validation:
Each mode produces appropriate output
Errors are caught and logged
Set options don’t break scripts
Performance is reasonable
Logs are readable and useful
Bonus:
Add assertions/invariant checking
Implement breakpoint system
Add memory usage tracking
Create performance profile report
14.5.4. Lab 3: Advanced Array Processing Pipeline#
Create a data processing pipeline that uses arrays for data transformation and analysis.
Requirements:
Load data from CSV into indexed array
Parse and validate data (type checking)
Transform data using array operations
Filter and sort using arrays
Generate statistics from array data
Export results to multiple formats
Handle large datasets efficiently
Sample Data:
id,name,department,salary,start_date
1,Alice,Engineering,120000,2020-01-15
2,Bob,Sales,80000,2021-03-20
3,Charlie,Engineering,130000,2019-06-10
4,Diana,Marketing,75000,2022-01-05
Processing Operations:
Parse CSV into 2D array structure
Filter by department (Engineering only)
Sort by salary (descending)
Calculate statistics: avg salary, count, range
Generate summary report
Export to JSON, XML, and formatted text
Expected Output:
=== Employee Report ===
Total Employees: 4
Engineering: 2 employees
Average Salary: $125,000
Salary Range: $120,000 - $130,000
Marketing: 1 employee
Average Salary: $75,000
Sales: 1 employee
Average Salary: $80,000
Validation:
CSV parsing is accurate
Data types are validated
Filtering works correctly
Sorting produces correct order
Statistics are accurate
All export formats are valid
Bonus:
Support complex queries (multiple filters)
Implement GROUP BY operations
Add data aggregation functions
Create pivot tables from array data
14.5.5. Lab 2: Template Generation with Here-Documents#
Create a template system that generates configuration files, HTML, and scripts from here-documents.
Requirements:
Create reusable templates for different file types
Generate HTML reports from template
Generate shell scripts from template
Generate configuration files (nginx, app config)
Support variable substitution in templates
Create multiple files from single script
Validate generated files
Template Types:
HTML Report template with metrics
Nginx configuration template
Systemd service file template
Application config template
Database initialization script
Example Usage:
generate_html_report "Production Server" "12:30 PM"
generate_nginx_config "example.com" 3000
generate_systemd_service "myapp"
generate_db_init "users" "products"
Expected Output Files:
report.html (with timestamp, server info, metrics)
nginx-example.com.conf (ready to use)
myapp.service (systemd service file)
init.sql (database initialization)
Validation:
All templates generate valid output
Variable substitution works correctly
Generated files have correct syntax
Generated files are executable/readable as appropriate
Multiple templates can be generated in sequence
Bonus:
Support template inheritance
Add conditional sections in templates
Implement template escaping for special chars
Add template versioning/history
14.5.6. Lab 1: Array-Based Configuration System#
Build a configuration management system using arrays to store and retrieve settings.
Requirements:
Create indexed array with application settings
Implement associative array for user preferences
Create functions to get/set configuration values
Support default values when key doesn’t exist
Validate configuration values
Generate configuration report
Save and load configuration from file
Configuration Data:
# Indexed array
declare -a app_settings=(
"version=1.0.0"
"debug=true"
"log_level=info"
"max_workers=4"
)
# Associative array
declare -A user_prefs=(
[theme]="dark"
[editor]="vim"
[shell]="bash"
)
Expected Functions:
get_config(key, [default_value])
set_config(key, value)
validate_config()
save_config(filename)
load_config(filename)
print_config_report()
Output Example:
=== Application Configuration ===
Version: 1.0.0
Debug: true
Log Level: info
Max Workers: 4
=== User Preferences ===
Theme: dark
Editor: vim
Shell: bash
Configuration is valid ✓
Validation:
Configuration values are stored and retrieved correctly
Default values work when key missing
Invalid values are rejected
Configuration can be saved to file
Configuration can be loaded from file
Configuration report is readable
Bonus:
Implement INI file format parsing
Support nested configuration (arrays of objects)
Add configuration merging (base + override)
Environment variable override support