一键导入
shell-scripting
Use when writing bash/shell scripts - covers structured output, dependency management, command variants (GNU vs BSD), and avoiding reinvented wheels
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Use when writing bash/shell scripts - covers structured output, dependency management, command variants (GNU vs BSD), and avoiding reinvented wheels
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
MUST USE for ANY git operations. Enforces GPG-signed commits (-S flag always required; stop and ask user to unlock GPG agent on failure) and 50/72 commit message rule. Triggers: 'commit', 'rebase', 'squash', 'who wrote', 'when was X added', 'find the commit that'.
You MUST use this before any creative work - creating features, building components, adding functionality, or modifying behavior. Explores user intent, requirements and design before implementation.
Use when facing 2+ independent tasks that can be worked on without shared state or sequential dependencies
Use when you have a written implementation plan to execute in a separate session with review checkpoints
Use when implementation is complete, all tests pass, and you need to decide how to integrate the work - guides completion of development work by presenting structured options for merge, PR, or cleanup
Use when executing implementation plans with independent tasks in the current session
基于 SOC 职业分类
| name | shell-scripting |
| description | Use when writing bash/shell scripts - covers structured output, dependency management, command variants (GNU vs BSD), and avoiding reinvented wheels |
Write robust, portable, dependency-aware shell scripts. Core principle: Prefer structured output over text parsing, check dependencies explicitly, use existing commands over custom implementations.
Use this skill when:
Symptoms:
grep, sed, awk, cut to parse command outputMANDATORY: Use long options (--option) for readability, EXCEPT when short option is POSIX standard and long option is GNU-only extension.
Why long options:
tar --create --gzip --file is clearer than tar -czfDecision tree:
Is there a --long option?
NO → Use short option
YES → Is short option POSIX standard and long option GNU-only?
YES → Use POSIX short (portable across Linux/BSD/macOS)
NO → Use GNU long (readability wins)
Quick examples:
| Command | Use This | Not This | Why |
|---|---|---|---|
ls | ls -a -l -h | ls --all --long | POSIX short, GNU long |
grep | grep -r -i | grep --recursive --ignore-case | POSIX short, GNU long |
tar | tar --create --gzip --file=x.tar.gz | tar -czf x.tar.gz | Both support long, clarity wins |
mkdir | mkdir --parents | mkdir -p | Widely supported long option |
find | find --type f --name "*.log" | find -type f -name "*.log" | GNU long preferred |
Acceptable short options:
tar -C, find -print0set -euo pipefail, rm -rf-a, -l, -h, -r, -i, -E, -n, -vNOT acceptable excuses:
Self-check (4 steps):
--long version exist? Check man command-short POSIX and --long GNU-only? Check BSD man pageCommands often provide machine-readable formats. Always check before parsing.
| ❌ Text Parsing | ✅ Structured Output |
|---|---|
lsblk | tail -n +3 | cut -d' ' -f1 | lsblk --json --output name,size |
df -h | grep pattern | awk '{print $5}' | df --output=source,pcent |
ps aux | grep pattern | awk '{print $2}' | pgrep -f pattern |
Check for these flags:
-J, --json: JSON output (lsblk, ip, systemctl)--output=: Column selection (df, ps, dpkg)-0, -z: Null-delimited (find, xargs, grep)-n, --noheadings: Remove headers (lsblk, lvs, pvs)Before writing custom logic, search for built-in commands.
| ❌ Custom | ✅ Existing |
|---|---|
basename "$part" | sed 's/[0-9]*$//' | lsblk --noheadings --output pkname "$part" |
| Loop to count files | find . --type f -printf '.' | wc --bytes |
| Parse /proc/meminfo | free --bytes or vmstat -s |
How to discover:
man command # Read manual for options
apropos "keyword" # Search for related tools
command --help | grep json # Check for structured output
Pattern:
#!/usr/bin/env bash
set -euo pipefail
# Required: jq, curl, lsblk
# Optional: notify-send (desktop notifications)
for cmd in jq curl lsblk; do
if ! command -v "$cmd" &>/dev/null; then
echo "Error: $cmd required" >&2
echo "Install: sudo apt install $cmd" >&2
exit 1
fi
done
Don't restrict to POSIX-only. Use GNU features when beneficial, but handle portability.
GNU-required pattern:
# This script requires GNU coreutils
# macOS: brew install coreutils
if ! stat --version 2>/dev/null | grep --quiet GNU; then
echo "Error: GNU stat required" >&2
echo "macOS: brew install coreutils" >&2
exit 1
fi
Cross-platform pattern:
if stat --version 2>/dev/null | grep --quiet GNU; then
file_time=$(stat --format='%Y' "$file") # GNU
else
file_time=$(stat -f '%m' "$file") # BSD/macOS
fi
POSIX short options (use these for portability):
| Command | POSIX Options | Avoid GNU Long |
|---|---|---|
ls | -a -l -h -t -r | --all --long --human-readable |
grep | -r -i -E -n -v | --recursive --ignore-case |
df | -h -T | --human-readable --type |
ps | -e -f -o | (no long options in POSIX) |
GNU long options (use these when both support or GNU-only):
| Command | Preferred | Reason |
|---|---|---|
tar | --create --gzip --file | Both GNU/BSD support, clarity wins |
mkdir | --parents | Widely available |
find | --printf | GNU-only feature, no POSIX equivalent |
| Command | Flag | Example |
|---|---|---|
lsblk | -J (JSON) | lsblk --json --output name,size |
ip | -j (JSON) | ip --json addr show |
df | --output= | df --output=source,pcent |
ps | -o | ps -eo pid,comm --no-headers |
find | -print0 | find . -print0 | xargs -0 |
# Single command
command -v jq &>/dev/null || { echo "jq required" >&2; exit 1; }
# Multiple commands
for cmd in jq curl awk; do
command -v "$cmd" &>/dev/null || { echo "$cmd required" >&2; exit 1; }
done
# Check for GNU
if cmd --version 2>/dev/null | grep --quiet GNU; then
# GNU code
else
# BSD code
fi
for file in $(ls *.txt); do echo "$file"; done # BREAKS on spaces
Fix: Use globs or find -print0
result=$(echo '{}' | jq -r '.key') # Fails silently if jq missing
Fix: Check with command -v jq first
df -h | grep sda1 | awk '{print $5}' # Fragile
Fix: df --output=pcent /dev/sda1
for i in {1..5}; do curl "$url" && break; sleep 2; done # Custom retry
Fix: timeout 30 bash -c 'until curl "$1"; do sleep 2; done' _ "$url"
STOP if you're about to:
--long.grep/sed/awk on output → Check for --json or --output= firstlsblk, blkid, findmnt firstman and apropos first| Excuse | Reality |
|---|---|
| "Short options faster to type" | 2 sec typing vs hours debugging later |
| "Just a one-liner" | One-liners get read and debugged too |
| "Emergency situation" | Emergencies need clarity MOST |
| "Existing code uses short" | New code uses proper style |
| "Regex simpler than JSON" | Regex breaks across versions, JSON doesn't |
| "Don't need dependency check" | Explicit errors save hours of debugging |
| "Text parsing works in tests" | Production has different locales/versions |
| "Already implemented, testing enough" | Testing proves now, not future-proof |
Patterns this skill addresses:
--output=)