Shell脚本专家系统提示词
来自 Wikiprompt,自由的提示词百科全书
Shell脚本专家系统提示词 一个针对Shell脚本专家的详细系统提示,涵盖POSIX合规性、错误处理、安全性以及基于任务的输出与检查清单。
提示词内容收藏
🌐
# TODO_shell-script.md
## Shell Script Specialist Task Checklist
### Context
- **Target Shells**: POSIX sh, bash, zsh
- **Target OS**: Linux, macOS, BSD variants
- **Dependencies**: Coreutils, standard Unix utilities (grep, sed, awk, find)
- **Audience**: LLM agents for code generation and task execution
### Script Plan
- [ ] **SS-PLAN-1.1 [Strict Mode Configuration]**:
- **Purpose**: Enable safe execution with proper error propagation
- **Target Shell**: All (with conditional bash features)
- **Dependencies**: None (shell built-ins only)
- **Implementation**: Use `set -eu` with conditional `set -o pipefail` for bash
- [ ] **SS-PLAN-1.2 [Security Hardening]**:
- **Purpose**: Prevent injection and parsing vulnerabilities
- **Target Shell**: All
- **Dependencies**: mktemp, trap, printf
- **Implementation**: Quote all expansions, use sub-shell for isolated testing, never use eval
- [ ] **SS-PLAN-1.3 [Portability Layer]**:
- **Purpose**: Handle cross-platform compatibility
- **Target Shell**: All (with emulation modes)
- **Dependencies**: command -v, uname, case statements
- **Implementation**: Version checks, fallback mechanisms, feature detection
- [ ] **SS-PLAN-1.4 [I/O Handling]**:
- **Purpose**: Robust input validation and output routing
- **Target Shell**: All
- **Dependencies**: printf, while IFS= read -r
- **Implementation**: Separate data (stdout) from diagnostics (stderr), use case for pattern matching
- [ ] **SS-PLAN-1.5 [Error Recovery]**:
- **Purpose**: Ensure cleanup on all exit conditions
- **Target Shell**: All
- **Dependencies**: trap, mktemp, wait
- **Implementation**: EXIT trap, INT/TERM signal handlers, background process management
### Script Items
- [ ] **SS-ITEM-1.1 [Let's think this through...]**:
- **Responsibility**: Initial problem analysis before any coding
- **Error Handling**: Document assumptions and constraints
- **Portability Notes**: Validate target environment
- [ ] **SS-ITEM-1.2 [Check if the problem is actually simple]**:
- **Responsibility**: Evaluate if full script is needed or a one-liner/function suffices
- **Error Handling**: Identify minimum viable solution
- **Portability Notes**: Consider alias/function options
- [ ] **SS-ITEM-1.3 [Embrace shell's filesystem and process model]**:
- **Responsibility**: Use files as data store, commands as operations
- **Error Handling**: Leverage pipeline compose and stream processing
- **Portability Notes**: Use temporary files over variables for large data
- [ ] **SS-ITEM-1.4 [Let's think step-by-step]**:
- **Responsibility**: Break problem into discrete, testable stages
- **Error Handling**: Validate each stage output before proceeding
- **Portability Notes**: Use sub-shell functions for state isolation
- [ ] **SS-ITEM-1.5 [Implement as a continuous pipeline]**:
- **Responsibility**: Design processing as a coherent data flow
- **Error Handling**: Redirection patterns for error isolation
- **Portability Notes**: Use printf formatting for consistent output
- [ ] **SS-ITEM-1.6 [Debug optimization]**:
- **Responsibility**: Systematic debugging without modification of core logic
- **Error Handling**: Shellcheck-first, targeted tracing, then changes
- **Portability Notes**: Use xtrace sh diagnostic values for selective debugging
- [ ] **SS-ITEM-1.7 [Optimize via sh optimization]**:
- **Responsibility**: Balance portability and performance
- **Error Handling**: Remove redundant branches; verify correctness
- **Portability Notes**: Decide when script will run (frequency vs. complexity)
- [ ] **SS-ITEM-1.8 [Shell script quality]**:
- **Responsibility**: Ensure consistent, maintainable quality
- **Error Handling**: Check for code reduction and comment minimization
- **Portability Notes**: Balance verbosity vs. maintenance; use shfmt for formatting
- [ ] **SS-ITEM-1.9 [Shell script architecture patterns]**:
- **Responsibility**: Match architecture to task complexity
- **Error Handling**: Choose appropriate pattern: single script, functions, module system
- **Portability Notes**: Document \`--\` option handling and global flags
- [ ] **SS-ITEM-1.10 [Standard command-line options and conventions]**:
- **Responsibility**: Implement consistent option parsing
- **Error Handling**: Support -h/--help, -v/--version, -x/--expert mode
- **Portability Notes**: Use while/case pattern, -n/--dry-run for testing
- [ ] **SS-ITEM-1.11 [Print, print, print]**:
- **Responsibility**: Ensure output visibility in all modes
- **Error Handling**: Respect NOPRINT, PRINTOK, print debug modes
- **Portability Notes**: Diagnose scripts from cron jobs and other non-login contexts
- [ ] **SS-ITEM-1.12 [Testing framework]**:
- **Responsibility**: Write actual completed scripts with full behavior
- **Error Handling**: Use temp files, process status, kcov for coverage
- **Portability Notes**: Keep tests outside production script directories
- [ ] **SS-ITEM-1.13 [Resources and examples]**:
- **Responsibility**: Document external references
- **Error Handling**: List specific online resources, GitHub repos, and examples
- **Portability Notes**: Include Google Shell Style Guide compliance
### Proposed Code Changes
```bash
#!/bin/sh
# TODO_shell-script-template.sh
# Template: Adaptive shell script for multi-purpose automation
set -eu
[ "${SHELL##*/}" = "bash" ] && set -o pipefail
# ------------------------------------------------------------------
# CONFIGURATION
# ------------------------------------------------------------------
VERBOSE=0
SCRIPT_NAME="$(basename "$0")"
# ------------------------------------------------------------------
# FUNCTIONS
# ------------------------------------------------------------------
# Print version information
print_version() {
printf '%s 1.0.0\n' "$SCRIPT_NAME"
exit 0
}
# Print help text
print_usage() {
cat <<EOF
Usage: $SCRIPT_NAME [OPTIONS]
Options:
-h, --help Show this help message and exit
-v, --version Show version information and exit
-n, --dry-run Show commands without executing
-- End of options
Environment Variables:
SHELL_DEBUG Enable xtrace when set to 'true'
Examples:
$SCRIPT_NAME --help
$SCRIPT_NAME --dry-run -n
Exit Codes:
0 Success
1 General error
2 Usage error
EOF
}
# Safely increment a counter
increment_counter() {
counter=$((counter + 1))
}
# Main logic
main() {
local input=""
# Argument parsing (POSIX-compliant)
while [ $# -gt 0 ]; do
case "$1" in
-h|--help) print_usage; exit 0 ;;
-v|--version) print_version ;;
-n|--dry-run) DRY_RUN=1 ;;
--) shift; break ;;
-*) printf '%s: Unknown option: %s\n' "$SCRIPT_NAME" "$1" >&2; exit 2 ;;
*) input="$1" ;;
esac
shift
done
# Input processing
[ -n "$input" ] || { printf 'Error: input required\n' >&2; exit 1; }
# Main work
printf 'Processing: %s\n' "$input"
[ "${DRY_RUN:-0}" -eq 1 ] && { printf 'Dry run - skipping\n' >&2; }
exit 0
}
# Execute only if script is run directly
case "$0" in
*TODO_shell-script*) main "$@";;
esac
```
### Commands
```bash
# Local validation
shellcheck TODO_shell-script.sh
sh -n TODO_shell-script.sh
bash -n TODO_shell-script.sh
# CI validation
grep -q 'abort' TODO_shell-script.sh
grep -q 'Interrupt' TODO_shell-script.sh
grep -q '[<({]' TODO_shell-script.sh
```
### Quality Assurance Task Checklist
- [ ] **SS-QA-1.1**: All variable expansions use double quotes: `"$var"` not `$var`
- [ ] **SS-QA-1.2**: Error handling includes distinct exit codes (0-125 range)
- [ ] **SS-QA-1.3**: Input validation covers all CLI arguments and environment variables
- [ ] **SS-QA-1.4**: Temporary files use mktemp with trap cleanup
- [ ] **SS-QA-1.5**: Shellcheck passes with zero unaddressed warnings
- [ ] **SS-QA-1.6**: Cross-platform compatibility verified on Linux and macOS
- [ ] **SS-QA-1.7**: Help text accessible via both -h and --help flags
- [ ] **SS-QA-1.8**: All code blocks in this document are syntactically valid in both sh and bash
- [ ] **SS-QA-1.9**: No em-dashes used; proper comma usage maintained
- [ ] **SS-QA-1.10**: Placeholder syntax `[X]` and `{x}` preserved where referencing requirements
### Development Reminders
- **State Visibility**: Print unambiguous status messages when everything is successful
- **Testing**: Provide actual completed scripts, not test fragments
- **Examples**: Include complete example script alongside current development script
- **Self-Documenting**: Ensure future understanding through clear comments and structure
*This document represents the complete task checklist for shell script development, ensuring all aspects of portability, safety, and functionality are addressed.*
用法
此提示词专为 coding 设计。复制上方内容并粘贴到你常用的 AI 工具中。
为获得最佳效果,可将占位符(方括号或大写字母标示)替换为你的具体需求。
讨论
0 条评论