Scripts Directory
Scripts Directory
Automation scripts for the Matt Blogs IT blog.
Available Scripts
1
optimize-images.sh
1 | optimize-images.sh |
Purpose: Batch optimize all images in directory1
assets/img
Features:
- Compresses JPEGs and PNGs
- Converts large images to WebP format
- Creates backup before optimization
- Provides detailed optimization report
- Respects 500KB size limit
Usage:
1
./scripts/optimize-images.sh
Requirements:
- ImageMagick (required)
- OptiPNG (optional but recommended)
- jpegoptim (optional but recommended)
- WebP tools (optional but recommended)
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:
- Creates backup in
1
_backup/images_<timestamp>
- Resizes images >1200px wide
- Compresses JPEGs to 85% quality
- Optimizes PNGs losslessly
- Converts large images (>100KB) to WebP
- Generates size report
Safety:
- Always creates backup before modifying
- Prompts for confirmation
- Provides restore commands
1
setup-git-hooks.sh
1 | setup-git-hooks.sh |
Purpose: Configure git to use custom hooks from directory1
.githooks
Features:
- Sets
to1
core.hooksPath
1
.githooks
- Validates hook installation
- Lists available hooks
Usage:
1
./scripts/setup-git-hooks.sh
Requirements:
- Git 2.9+ (for
support)1
core.hooksPath
What it does:
- Configures git to use
directory1
.githooks
- Displays installed hooks
- Shows usage instructions
Installed Hooks:
- Validates image sizes before commit1
pre-commit
Script Development
Adding New Scripts
- Create script in
directory1
scripts/
- Make executable:
1
chmod +x scripts/new-script.sh
- Add shebang:
1
#!/bin/bash
- Use
for error handling1
set -e
- Add color output for better UX
- 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
Related Documentation
- IMAGE_OPTIMIZATION.md - Image optimization workflow
- FUTURE_ENHANCEMENTS.md - Planned improvements
- CLAUDE.md - Development guidelines