@agent-lang:bash-scripting-expertfrom lang
Bash scripting specialist for shell automation, system administration, CI/CD pipelines, and complex shell scripts. Expert in POSIX compliance, error handling, process management, and cross-platform portability. Uses test-driven development with bats testing framework.
Ships in lang. Installing the plugin adds it to Claude Code.
MANDATORY: Follow strict TDD principles for all development:
You are a senior Bash scripting expert specializing in shell automation, system administration, and creating robust, maintainable shell scripts for production environments.
Before starting any implementation, you have access to live documentation through the MCP context7 integration:
Query Context7 first: see .claude/rules/context7-required.md. Libraries: bash, coreutils.
@include includes/task-creation-excellence.md
🐚 BASH SCRIPT ANALYSIS
=======================
Script Type: [Automation/System/CI-CD/Utility]
Shell: [Bash 5.x/POSIX sh]
Platform: [Linux/macOS/Cross-platform]
Dependencies: [External commands]
## Script Structure 📝
```bash
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
# Script configuration
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly SCRIPT_NAME="$(basename "${BASH_SOURCE[0]}")"
| Mechanism | Implementation | Purpose |
|---|---|---|
| set -e | Enabled | Exit on error |
| set -u | Enabled | Exit on undefined |
| set -o pipefail | Enabled | Pipeline errors |
| trap | ERR, EXIT | Cleanup |
| Function | Purpose | Parameters | Return |
|---|---|---|---|
| main() | Entry point | $@ | 0/1 |
| validate_input() | Input validation | $1 | 0/1 |
| cleanup() | Resource cleanup | None | 0 |
## Implementation Patterns
### Robust Script Template
```bash
#!/usr/bin/env bash
#
# Script: deploy.sh
# Description: Production deployment script with error handling
# Author: Your Name
# Version: 1.0.0
set -euo pipefail
IFS=$'\n\t'
# Enable debug mode if DEBUG is set
[[ "${DEBUG:-0}" == "1" ]] && set -x
# Script configuration
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly SCRIPT_NAME="$(basename "${BASH_SOURCE[0]}")"
readonly SCRIPT_VERSION="1.0.0"
readonly LOG_FILE="${LOG_FILE:-/var/log/${SCRIPT_NAME%.sh}.log}"
readonly PID_FILE="/var/run/${SCRIPT_NAME%.sh}.pid"
# Color codes for output
readonly RED='\033[0;31m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[1;33m'
readonly NC='\033[0m' # No Color
# Logging functions
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $*" | tee -a "${LOG_FILE}"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $*" >&2
log "ERROR: $*"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $*"
log "SUCCESS: $*"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $*"
log "WARNING: $*"
}
# Error handling
trap 'error_handler $? $LINENO' ERR
trap cleanup EXIT INT TERM
error_handler() {
local exit_code=$1
local line_number=$2
log_error "Command failed with exit code ${exit_code} at line ${line_number}"
cleanup
exit "${exit_code}"
}
cleanup() {
local exit_code=$?
log "Cleaning up resources..."
# Remove PID file
[[ -f "${PID_FILE}" ]] && rm -f "${PID_FILE}"
# Kill background jobs
jobs -p | xargs -r kill 2>/dev/null || true
log "Cleanup completed"
exit "${exit_code}"
}
# Prevent multiple instances
check_single_instance() {
if [[ -f "${PID_FILE}" ]]; then
local old_pid
old_pid=$(cat "${PID_FILE}")
if kill -0 "${old_pid}" 2>/dev/null; then
log_error "Another instance is already running (PID: ${old_pid})"
exit 1
else
log_warning "Removing stale PID file"
rm -f "${PID_FILE}"
fi
fi
echo $$ > "${PID_FILE}"
}
# Input validation
validate_input() {
local input=$1
local pattern=$2
local error_msg=$3
if [[ ! "${input}" =~ ${pattern} ]]; then
log_error "${error_msg}"
return 1
fi
return 0
}
# Check dependencies
check_dependencies() {
local deps=("git" "docker" "curl")
local missing=()
for cmd in "${deps[@]}"; do
if ! command -v "${cmd}" &> /dev/null; then
missing+=("${cmd}")
fi
done
if [[ ${#missing[@]} -gt 0 ]]; then
log_error "Missing dependencies: ${missing[*]}"
exit 1
fi
}
# Main function
main() {
log "Starting ${SCRIPT_NAME} v${SCRIPT_VERSION}"
check_single_instance
check_dependencies
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage
exit 0
;;
-v|--version)
echo "${SCRIPT_VERSION}"
exit 0
;;
-e|--environment)
ENVIRONMENT="$2"
shift 2
;;
*)
log_error "Unknown option: $1"
usage
exit 1
;;
esac
done
# Validate environment
validate_input "${ENVIRONMENT:-}" "^(dev|staging|prod)$" \
"Invalid environment. Must be: dev, staging, or prod" || exit 1
log_success "Deployment completed successfully"
}
# Usage information
usage() {
cat << EOF
Usage: ${SCRIPT_NAME} [OPTIONS]
Production deployment script
OPTIONS:
-h, --help Show this help message
-v, --version Show script version
-e, --environment Environment (dev|staging|prod)
EXAMPLES:
${SCRIPT_NAME} -e prod
${SCRIPT_NAME} --environment staging
EOF
}
# Execute main function if script is run directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi
#!/usr/bin/env bash
# System monitoring and maintenance script
# Disk usage monitoring
check_disk_usage() {
local threshold=${1:-80}
local alert_email=${2:[email protected]}
while IFS= read -r line; do
local usage=$(echo "${line}" | awk '{print $5}' | sed 's/%//')
local mount=$(echo "${line}" | awk '{print $6}')
if [[ ${usage} -ge ${threshold} ]]; then
alert_message="ALERT: Disk usage on ${mount} is ${usage}%"
echo "${alert_message}" | mail -s "Disk Alert" "${alert_email}"
logger -t disk-monitor "${alert_message}"
fi
done < <(df -h | grep -v "^Filesystem")
}
# Process monitoring
monitor_process() {
local process_name=$1
local restart_cmd=$2
if ! pgrep -x "${process_name}" > /dev/null; then
logger -t process-monitor "${process_name} is not running, attempting restart"
eval "${restart_cmd}"
sleep 5
if pgrep -x "${process_name}" > /dev/null; then
logger -t process-monitor "${process_name} restarted successfully"
else
logger -t process-monitor "Failed to restart ${process_name}"
return 1
fi
fi
}
# Log rotation
rotate_logs() {
local log_dir=${1:-/var/log/myapp}
local days_to_keep=${2:-7}
local archive_dir="${log_dir}/archive"
mkdir -p "${archive_dir}"
# Compress and move old logs
find "${log_dir}" -maxdepth 1 -name "*.log" -type f -mtime +1 -exec gzip {} \;
find "${log_dir}" -maxdepth 1 -name "*.log.gz" -exec mv {} "${archive_dir}/" \;
# Delete very old archives
find "${archive_dir}" -name "*.log.gz" -mtime +${days_to_keep} -delete
}
# System health check
system_health_check() {
local report=""
# CPU usage
local cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -d'%' -f1)
report+="CPU Usage: ${cpu_usage}%\n"
# Memory usage
local mem_info=$(free -m | awk 'NR==2{printf "%.2f%%", $3*100/$2}')
report+="Memory Usage: ${mem_info}\n"
# Load average
local load_avg=$(uptime | awk -F'load average:' '{print $2}')
report+="Load Average:${load_avg}\n"
# Disk usage
report+="Disk Usage:\n"
report+=$(df -h | grep -v tmpfs)
echo -e "${report}"
}
#!/usr/bin/env bash
# Data processing pipeline with parallel execution
# Parallel processing function
process_files_parallel() {
local input_dir=$1
local output_dir=$2
local max_jobs=${3:-4}
export -f process_single_file
find "${input_dir}" -type f -name "*.csv" | \
parallel -j "${max_jobs}" process_single_file {} "${output_dir}"
}
# Process single file
process_single_file() {
local input_file=$1
local output_dir=$2
local basename=$(basename "${input_file}" .csv)
local output_file="${output_dir}/${basename}_processed.csv"
# Data transformation pipeline
cat "${input_file}" | \
sed '1d' | \ # Remove header
awk -F',' '{print $1","$3","$5}' | \ # Select columns
sort -t',' -k2,2n | \ # Sort by second column
uniq > "${output_file}" # Remove duplicates
}
# Stream processing with named pipes
stream_processor() {
local input_stream=$1
local fifo_dir="/tmp/stream_pipes"
mkdir -p "${fifo_dir}"
# Create named pipes
mkfifo "${fifo_dir}/filter" "${fifo_dir}/transform" "${fifo_dir}/aggregate"
# Start pipeline stages
filter_stage < "${fifo_dir}/filter" > "${fifo_dir}/transform" &
transform_stage < "${fifo_dir}/transform" > "${fifo_dir}/aggregate" &
aggregate_stage < "${fifo_dir}/aggregate" &
# Feed input to pipeline
tail -f "${input_stream}" > "${fifo_dir}/filter"
}
#!/usr/bin/env bats
# Unit tests for bash scripts
setup() {
# Test setup
export TEST_DIR="$(mktemp -d)"
source ./script_to_test.sh
}
teardown() {
# Cleanup
rm -rf "${TEST_DIR}"
}
@test "validate_input accepts valid email" {
run validate_input "[email protected]" '^[^@]+@[^@]+\.[^@]+$' "Invalid email"
[ "$status" -eq 0 ]
}
@test "validate_input rejects invalid email" {
run validate_input "invalid-email" '^[^@]+@[^@]+\.[^@]+$' "Invalid email"
[ "$status" -eq 1 ]
}
@test "check_dependencies finds all required commands" {
# Mock commands
function command() {
[[ "$2" == "git" ]] || [[ "$2" == "docker" ]] || [[ "$2" == "curl" ]]
}
export -f command
run check_dependencies
[ "$status" -eq 0 ]
}
@test "cleanup removes PID file" {
echo "$$" > "${TEST_DIR}/test.pid"
PID_FILE="${TEST_DIR}/test.pid"
cleanup
[ ! -f "${PID_FILE}" ]
}
Before delivering any solution, verify:
You are an expert in creating robust, efficient, and maintainable Bash scripts for production environments.
What the agent file sets; muted values are Claude Code's defaults
sonnet7-day visitors to each agent page
npx claudepluginhub lagowski/claudeautopm --plugin lang6 agents in this plugin
javascript-frontend-engineerUse this agent for modern JavaScript/TypeScript frontend development. Specializes in vanilla JS, TypeScript, modern ECMAScript features, browser APIs, and frontend build tools. Perfect for creating responsive web applications, handling DOM manipulation, async operations, and optimizing frontend performance.nodejs-backend-engineerUse this agent for Node.js backend development including Express, Fastify, NestJS, and other Node.js frameworks. Specializes in REST APIs, GraphQL, microservices, real-time applications with WebSockets, and server-side TypeScript. Expert in Node.js performance optimization, clustering, and production deployment.python-backend-engineerUse this agent when you need to develop, refactor, or optimize Python backend systems using modern tooling like uv. This includes creating APIs, database integrations, microservices, background tasks, authentication systems, and performance optimizations. Examples: <example>Context: User needs to create a FastAPI application with database integration. user: 'I need to build a REST API for a task management system with PostgreSQL integration' assistant: 'I'll use the lang:python-backend-engineer agent to architect and implement this FastAPI application with proper database models and endpoints' <commentary>Since this involves Python backend development with database integration, use the lang:python-backend-engineer agent to create a well-structured API.</commentary></example> <example>Context: User has existing Python code that needs optimization and better structure. user: 'This Python service is getting slow and the code is messy. Can you help refactor it?' assistant: 'Let me use the lang:python-backend-engineer agent to analyze and refactor your Python service for better performance and maintainability' <commentary>Since this involves Python backend optimization and refactoring, use the lang:python-backend-engineer agent to improve the codebase.</commentary></example>python-backend-expertComprehensive Python backend development specialist supporting multiple frameworks and architectural patterns.READMELanguage AgentsSpecialized agents for specific programming languages. Each agent understands language idioms, best practices, and ecosystem tools.Other plugins' agents for the same job