Shell Script Validation
This skill provides guidance for validating shell scripts using the comprehensive validation script.
When to Use This Skill
This skill is applicable for:
- •Validating shell script before committing
- •Running comprehensive script quality checks
- •Ensuring syntax correctness
- •Verifying best practice compliance
⚠️ CRITICAL: Always Use the Validation Script
DO NOT run individual commands (bash -n, shellcheck) directly.
The validation script handles everything automatically.
Usage
# Run all validations in the workspace bash shell-script-validation/scripts/validate.sh # Validate a specific directory bash shell-script-validation/scripts/validate.sh ./scripts/new_feature/ # Validate a specific script bash shell-script-validation/scripts/validate.sh ./scripts/deploy.sh # With verbose output and auto-fix bash shell-script-validation/scripts/validate.sh -v -f
What the Script Does
The validation script performs all checks in the correct order:
- •
bash -n- Syntax check without execution - •
shellcheck- Comprehensive static analysis and best practice enforcement - •Project standards - Verify script template compliance
Validation Requirements
Before considering scripts complete:
- •✅ All validation checks pass
- •✅ No syntax errors
- •✅ No shellcheck warnings
- •✅ Follows project script standards
- •✅ Bats tests pass (if applicable)
Validation Workflow
Before Committing
- •Make changes - Edit shell scripts
- •Run validation:
bash
bash shell-script-validation/scripts/validate.sh ./script.sh
- •Fix issues - Address any failures
- •Run tests (if applicable):
bash
bats test/*.bats
- •Commit - Only commit when all checks pass
Common Failures & Quick Fixes
Syntax Errors
Error: bash -n failed script.sh: line 15: syntax error near unexpected token 'fi'
Fix: Correct the syntax error indicated in the message
shellcheck Errors
SC2086: Quote variables to prevent word splitting
Fix: Add quotes around variables
# Bad cd $dir # Good cd "$dir" || exit
Common shellcheck Issues
SC2006: Use $(...) instead of backticks
# Bad result=`command` # Good result=$(command)
SC2046: Quote command substitutions
# Bad for file in $(find . -name "*.txt"); do # Good find . -name "*.txt" | while read -r file; do
SC2086: Quote variables
# Bad echo $var # Good echo "$var"
SC2164: Use cd ... || exit
# Bad cd /some/path # Good cd /some/path || exit
Script Standards
Required Template Elements
Every script must include:
#!/bin/bash
# Error handling: exit on error, unset variable, or failed pipeline
set -euo pipefail
# Secure defaults
umask 027
export LC_ALL=C.UTF-8
# Get script directory for library loading
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
export SCRIPT_DIR
# Load all-in-one library
# shellcheck source=../lib/all.sh
# shellcheck disable=SC1091
source "${SCRIPT_DIR}/../lib/all.sh"
Function Order
Functions must be ordered as follows:
- •
show_usage/parse_arguments(if present) - •Other functions in alphabetical order
- •
mainfunction last
Troubleshooting
Validation Script Not Found
# Navigate to project root cd /workspace # Verify script exists ls -la .github/skills/shell-script-validation/scripts/validate.sh # Run with bash explicitly bash shell-script-validation/scripts/validate.sh
Permission Denied
# Run with bash explicitly bash shell-script-validation/scripts/validate.sh ./script.sh
Need More Details
For detailed information, see the reference documentation:
- •Individual Commands - Detailed command usage for debugging
- •Troubleshooting Guide - Comprehensive error resolution
- •Script Standards - Project script template and conventions
Quick Reference
Essential Commands
# Full validation bash shell-script-validation/scripts/validate.sh # Specific script bash shell-script-validation/scripts/validate.sh ./script.sh # With auto-fix bash shell-script-validation/scripts/validate.sh -f
Validation Checklist
Before committing:
- • Validation script passes
- • Bats tests pass (if applicable)
- • Follows project script standards
- • No security issues
Summary
Shell script validation ensures code quality and reliability:
- •Always use the validation script - Never run individual commands
- •Validate frequently - Run during development, not just before commit
- •Follow standards - Use project template and best practices
- •Test with Bats - Cover edge cases and errors (if applicable)
- •Ensure security - Validate inputs and quote variables
- •Never commit invalid scripts - All checks must pass
For detailed debugging and advanced topics, see the reference documentation.