================================================================================ WORK WITH ME WEDNESDAY - TROUBLESHOOTING ================================================================================ CLASS INFO: ----------- Topic: [TROUBLESHOOTING TOPIC - e.g., "Debugging Command Line Issues"] Date: [YYYY-MM-DD] Duration: [60 minutes] Level: [Intermediate/Advanced] Instructor: [NAME] LEARNING OBJECTIVES: -------------------- By the end of this session, students will be able to: • [Objective 1 - e.g., Read and interpret error messages] • [Objective 2 - e.g., Use debugging tools effectively] • [Objective 3 - e.g., Trace execution with verbose flags] • [Objective 4 - e.g., Resolve common permission and path issues] PREREQUISITES: -------------- • Basic command line proficiency • Experience running scripts or programs • Familiarity with text editing SETUP (5 MIN): -------------- 1. Create troubleshooting workspace: cd ~/Desktop mkdir debug-practice cd debug-practice 2. Install useful tools (if not present): # macOS: brew install curl jq # Linux: apt-get install curl jq HANDS-ON EXERCISES: ------------------- Exercise 1: [TITLE - e.g., "Reading Error Messages"] ---------------------------------------------------- Create broken script (broken.sh): #!/bin/bash echo "Starting..." cd /nonexistent/path echo "This won't run" Commands: chmod +x broken.sh ./broken.sh echo $? # Check exit code What to look for: - Error message text - Line numbers (if shown) - Exit codes (0 = success, non-zero = error) Expected learning: Parse error output systematically Exercise 2: [TITLE - e.g., "Verbose and Debug Flags"] ---------------------------------------------------- Commands: bash -x broken.sh # Show each command as executed set -x # Enable debug mode in script set -e # Exit on first error set -u # Exit on undefined variable Script with debugging: #!/bin/bash set -euxo pipefail # All flags enabled echo "Debug mode active" VAR="test" echo $VAR Expected outcome: See execution trace of each command Exercise 3: [TITLE - e.g., "Permission Issues"] ---------------------------------------------------- Commands: touch test.txt chmod 000 test.txt # Remove all permissions cat test.txt # Try to read - fails ls -l test.txt # Check permissions chmod 644 test.txt # Fix permissions cat test.txt # Success Debugging steps: - ls -l to check permissions - Check ownership with ls -l - Use sudo only when necessary - Fix with chmod/chown Expected outcome: Understand permission errors and fixes Exercise 4: [TITLE - e.g., "Path and Environment Issues"] ---------------------------------------------------- Commands: echo $PATH # Check PATH variable which python # Find command location type python # Alternative to which env # Show all environment variables export MY_VAR="value" # Set environment variable printenv MY_VAR # Check variable Common path fixes: export PATH="$HOME/bin:$PATH" Expected outcome: Diagnose "command not found" errors Exercise 5: [TITLE - e.g., "Log Analysis"] ---------------------------------------------------- Create log generator: #!/bin/bash echo "INFO: Starting process" >> app.log echo "WARN: Low memory" >> app.log echo "ERROR: Connection failed" >> app.log echo "INFO: Retrying..." >> app.log Analysis commands: grep "ERROR" app.log grep -c "ERROR" app.log # Count errors tail -f app.log # Follow log in real-time grep "ERROR\|WARN" app.log # Multiple patterns Expected outcome: Extract relevant information from logs COMMON ERRORS & SOLUTIONS: --------------------------- Error 1: "command not found" Root causes: - Typo in command name - Command not installed - Not in PATH Solutions: - which [command] to check if installed - Add to PATH: export PATH="$PATH:/new/path" - Install missing tool Error 2: "Permission denied" Root causes: - File not executable - Wrong file ownership - Directory permissions Solutions: - chmod +x [file] for scripts - chmod 755 [file] for broader access - Check ownership: ls -l Error 3: "No such file or directory" Root causes: - Wrong path (relative vs absolute) - Typo in filename - File doesn't exist yet Solutions: - pwd to check current location - ls to verify files - Use tab completion - Use absolute paths Error 4: "Syntax error" Root causes: - Missing quotes - Wrong spacing in conditionals - Unclosed brackets Solutions: - Use shellcheck for bash scripts - Check matching quotes/brackets - Validate syntax with bash -n script.sh DEBUGGING CHECKLIST: -------------------- □ Read the full error message carefully □ Note the line number (if provided) □ Check the exit code: echo $? □ Run with verbose flag (-v or -x) □ Verify file/directory exists □ Check permissions: ls -l □ Verify PATH and environment variables □ Search error message online □ Test with minimal example □ Check logs if available PRACTICE CHALLENGES: -------------------- 1. [Challenge 1 - e.g., "Fix 5 intentionally broken scripts"] 2. [Challenge 2 - e.g., "Debug a script that runs but produces wrong output"] 3. [Challenge 3 - e.g., "Trace execution of a complex pipeline"] CHEAT SHEET: ------------ Debugging Commands: bash -x script.sh # Show execution trace bash -n script.sh # Check syntax without running set -euxo pipefail # Strict mode for scripts echo $? # Last command exit code Permission Commands: ls -l # Check permissions chmod +x file # Make executable chmod 644 file # rw-r--r-- chmod 755 file # rwxr-xr-x chown user:group file # Change ownership Path & Environment: echo $PATH # View PATH which command # Find command location type command # Show command type env # All environment variables export VAR=value # Set environment variable Log Analysis: tail -f log.txt # Follow log in real-time grep "ERROR" log # Search logs grep -A 3 "ERROR" # Show 3 lines after match grep -B 3 "ERROR" # Show 3 lines before match less log.txt # Paginate through log Testing: curl -v url # Verbose HTTP request ping -c 4 host # Test connectivity nc -zv host port # Test port connectivity strace command # Trace system calls (Linux) RESOURCES & NEXT STEPS: ----------------------- • Explainshell.com - command breakdown • ShellCheck.net - bash script linter • Stack Overflow - search error messages • Man pages: man [command] • Command help: [command] --help • Next class: [TOPIC] INSTRUCTOR NOTES: ----------------- [Add specific notes, student questions, modifications made during class] ================================================================================