with one click
shell-scripting
Bash and Zsh scripting patterns with built-in logging and non-interactive execution. Use when writing automation scripts, CLI tools, or system administration tasks.
Menu
Bash and Zsh scripting patterns with built-in logging and non-interactive execution. Use when writing automation scripts, CLI tools, or system administration tasks.
AWS CLI best practices including pagination control, output formatting, and efficient querying. Use when running AWS CLI commands, querying AWS resources, or automating AWS operations.
AWS CloudWatch dashboard observability patterns including dashboard hierarchy, widget selection, CDK implementation, metric math, alarms, and cross-account monitoring. Use when creating dashboards, setting up observability, or instrumenting workloads with CloudWatch.
Docker image building best practices including multi-stage builds, layer optimization, and security hardening. Use when creating Dockerfiles, optimizing image size, or debugging container builds.
Technical documentation patterns including READMEs, API docs, runbooks, and architecture decision records. Use when writing documentation, creating runbooks, or documenting system architecture.
Git operations, branching strategies, commit conventions, and repository management. Use when working with version control, creating branches, writing commits, or resolving merge conflicts.
| name | shell-scripting |
| description | Bash and Zsh scripting patterns with built-in logging and non-interactive execution. Use when writing automation scripts, CLI tools, or system administration tasks. |
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_NAME="$(basename "$0")"
LOG_FILE="${LOG_FILE:-/tmp/${SCRIPT_NAME%.*}.log}"
log() { printf '%s [%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$1" "$2" | tee -a "$LOG_FILE"; }
info() { log "INFO" "$1"; }
warn() { log "WARN" "$1"; }
error() { log "ERROR" "$1" >&2; }
die() { error "$1"; exit 1; }
usage() {
cat <<EOF
Usage: $SCRIPT_NAME <required_arg> [optional_arg]
-h, --help Show this help
-y, --yes Skip confirmations (default: true)
-v, --verbose Enable debug output
EOF
exit 0
}
# Defaults: non-interactive
YES=true
VERBOSE=false
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help) usage ;;
-y|--yes) YES=true; shift ;;
-v|--verbose) VERBOSE=true; set -x; shift ;;
--) shift; break ;;
-*) die "Unknown option: $1" ;;
*) break ;;
esac
done
[[ $# -lt 1 ]] && die "Missing required argument. Use -h for help."
ARG1="$1"
ARG2="${2:-default_value}"
main() {
info "Starting: $ARG1"
# Your logic here
info "Completed successfully"
}
main "$@"
set -e # Exit on error
set -u # Error on undefined variables
set -o pipefail # Catch pipe failures
set -x # Debug mode (print commands)
# Auto-confirm dangerous commands
rm -rf "$DIR" # No -i flag
# Provide defaults instead of prompting
RESPONSE="${RESPONSE:-yes}"
# Use heredoc for stdin
mysql -u root <<< "SELECT 1;"
# Timeout commands that might hang
timeout 30 curl -s "$URL" || die "Request timed out"
# Skip interactive pagers
git --no-pager log -10
aws ec2 describe-instances --no-cli-pager
# Zsh-specific: extended globbing
setopt extended_glob
rm -f **/*.tmp(N) # (N) = nullglob, no error if no match
# Array handling (works in both)
arr=(one two three)
for item in "${arr[@]}"; do echo "$item"; done
# Zsh associative arrays
typeset -A map
map[key]=value
# Check command exists
command -v docker &>/dev/null || die "docker required"
# Cleanup on exit
cleanup() { [[ -f "$TMPFILE" ]] && rm -f "$TMPFILE"; }
trap cleanup EXIT
TMPFILE=$(mktemp)
# Retry with backoff
retry() {
local n=0 max=3 delay=2
while ! "$@"; do
((n++)) && ((n >= max)) && return 1
info "Retry $n/$max in ${delay}s..."
sleep $delay
((delay *= 2))
done
}
# Parallel execution
parallel_run() {
local pids=()
for cmd in "$@"; do
eval "$cmd" & pids+=($!)
done
for pid in "${pids[@]}"; do wait "$pid" || return 1; done
}
${var:0:5} # First 5 chars
${var: -3} # Last 3 chars
${var/old/new} # Replace first
${var//old/new} # Replace all
${var#prefix} # Remove prefix
${var%suffix} # Remove suffix
${var:-default} # Default if unset
${var:?error} # Error if unset
[[ -f "$file" ]] # File exists
[[ -d "$dir" ]] # Directory exists
[[ -z "$var" ]] # Variable empty
[[ -n "$var" ]] # Variable not empty
[[ "$a" == "$b" ]] # String equality
[[ "$a" -eq "$b" ]] # Numeric equality
[[ -t 0 ]] # stdin is terminal (interactive)