Scripts Directory

Scripts Directory

Automation scripts for the Matt Blogs IT blog.

Available Scripts

1
optimize-images.sh

Purpose: Batch optimize all images in

1
assets/img
directory

Features:

Usage:

1
./scripts/optimize-images.sh

Requirements:

Installation:

1
2
3
4
5
# macOS
brew install imagemagick optipng jpegoptim webp

# Ubuntu/Debian
sudo apt-get install imagemagick optipng jpegoptim webp

What it does:

  1. Creates backup in
    1
    
    _backup/images_<timestamp>
    
  2. Resizes images >1200px wide
  3. Compresses JPEGs to 85% quality
  4. Optimizes PNGs losslessly
  5. Converts large images (>100KB) to WebP
  6. Generates size report

Safety:


1
setup-git-hooks.sh

Purpose: Configure git to use custom hooks from

1
.githooks
directory

Features:

Usage:

1
./scripts/setup-git-hooks.sh

Requirements:

What it does:

  1. Configures git to use
    1
    
    .githooks
    
    directory
  2. Displays installed hooks
  3. Shows usage instructions

Installed Hooks:


Script Development

Adding New Scripts

  1. Create script in
    1
    
    scripts/
    
    directory
  2. Make executable:
    1
    
    chmod +x scripts/new-script.sh
    
  3. Add shebang:
    1
    
    #!/bin/bash
    
  4. Use
    1
    
    set -e
    
    for error handling
  5. Add color output for better UX
  6. Document in this README

Script Guidelines

Error Handling:

1
2
3
set -e  # Exit on error
set -u  # Exit on undefined variable
set -o pipefail  # Exit on pipe failures

Color Output:

1
2
3
4
5
6
7
8
9
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo -e "${GREEN}✓ Success message${NC}"
echo -e "${RED}✗ Error message${NC}"
echo -e "${YELLOW}⚠ Warning message${NC}"

User Confirmation:

1
2
3
4
5
6
read -p "Continue? (y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "Cancelled."
    exit 0
fi

Testing Scripts

1
2
3
4
5
6
7
8
# Check syntax without executing
bash -n scripts/script-name.sh

# Run with verbose output
bash -x scripts/script-name.sh

# Test with ShellCheck (if installed)
shellcheck scripts/script-name.sh

Troubleshooting

Permission Denied

1
2
# Make script executable
chmod +x scripts/script-name.sh

Command Not Found

1
2
3
4
5
# Run with explicit bash
bash scripts/script-name.sh

# Check if script is in correct directory
ls -la scripts/

Git Hooks Not Running

1
2
3
4
5
# Check hooks path configuration
git config core.hooksPath

# Re-run setup
./scripts/setup-git-hooks.sh