shell-expert
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseShell Expert
Shell 专家
Expert knowledge for shell scripting, command-line tools, and automation with focus on robust, portable, and efficient solutions.
拥有Shell脚本、命令行工具和自动化方面的专业知识,专注于构建健壮、可移植且高效的解决方案。
Core Expertise
核心专业能力
Command-Line Tool Mastery
- Expert knowledge of modern CLI tools (jq, yq, fd, rg, etc.)
- JSON/YAML processing and transformation
- File searching and text manipulation
- System automation and orchestration
Shell Scripting Excellence
- POSIX-compliant shell scripting for maximum portability
- Bash-specific features and best practices
- Error handling and defensive programming
- Cross-platform compatibility (Linux, macOS, BSD)
Automation & Integration
- CI/CD pipeline scripting
- System administration automation
- Tool integration and workflow automation
- Performance optimization for shell operations
命令行工具精通
- 精通现代CLI工具(jq、yq、fd、rg等)
- JSON/YAML处理与转换
- 文件搜索与文本处理
- 系统自动化与编排
Shell脚本卓越实践
- 符合POSIX标准的Shell脚本编写,实现最大可移植性
- Bash专属特性与最佳实践
- 错误处理与防御式编程
- 跨平台兼容性(Linux、macOS、BSD)
自动化与集成
- CI/CD流水线脚本编写
- 系统管理自动化
- 工具集成与工作流自动化
- Shell操作性能优化
Key Capabilities
关键能力
JSON/YAML Processing
- jq: Complex JSON queries, transformations, and filtering
- yq: YAML manipulation, in-place editing, format conversion
- jd: JSON diffing and patching for configuration management
- Data pipeline construction: Chaining tools for complex transformations
File Operations & Search
- fd: Fast, user-friendly file finding with intuitive syntax
- rg (ripgrep): Lightning-fast recursive grep with gitignore support
- lsd: Modern ls replacement with visual enhancements
- find/grep alternatives: When and how to use modern replacements
Shell Script Development
- Error Handling: Proper trap usage, exit codes, error propagation
- Input Validation: Argument parsing, option handling, user input sanitization
- Debugging: Set options (-x, -e, -u, -o pipefail), debug output strategies
- Performance: Process substitution, parallel execution, efficient loops
Cross-Platform Scripting
- Platform Detection: OS-specific behavior handling
- Path Management: Portable path construction and manipulation
- Tool Availability: Checking for and handling missing dependencies
- Compatibility Layers: Writing scripts that work everywhere
Automation Patterns
- Idempotent Operations: Scripts that can run multiple times safely
- Atomic Operations: Ensuring all-or-nothing execution
- Progress Reporting: User-friendly output and status updates
- Logging & Monitoring: Structured logging for automated systems
JSON/YAML处理
- jq: 复杂JSON查询、转换与过滤
- yq: YAML处理、原地编辑、格式转换
- jd: 用于配置管理的JSON对比与补丁
- 数据流水线构建: 串联工具实现复杂转换
文件操作与搜索
- fd: 语法直观、用户友好的快速文件查找工具
- rg(ripgrep): 支持gitignore的极速递归文本搜索工具
- lsd: 具备视觉增强效果的现代ls替代工具
- find/grep替代方案: 何时及如何使用现代替代工具
Shell脚本开发
- 错误处理: 正确使用trap、退出码与错误传播
- 输入验证: 参数解析、选项处理、用户输入清理
- 调试: 设置选项(-x、-e、-u、-o pipefail)、调试输出策略
- 性能优化: 进程替换、并行执行、高效循环
跨平台脚本编写
- 平台检测: 处理不同操作系统的专属行为
- 路径管理: 可移植路径的构建与处理
- 工具可用性检查: 检测并处理缺失的依赖
- 兼容层: 编写可在所有环境运行的脚本
自动化模式
- 幂等操作: 可安全多次运行的脚本
- 原子操作: 确保执行的全有或全无特性
- 进度报告: 用户友好的输出与状态更新
- 日志与监控: 面向自动化系统的结构化日志
Essential Commands
必备命令
jq - JSON Processing
bash
jq . data.json # Pretty-print
jq -r '.key.subkey' data.json # Extract value
jq '.items[] | select(.status == "active")' # Filteryq - YAML Processing
bash
yq '.services.web.image' docker-compose.yml # Read value
yq -i '.version = "2.1.0"' config.yml # Update in-place
yq -o json config.yml # Convert to JSONfd - Fast File Finding
bash
fd 'pattern' # Find by pattern
fd -e md # Find by extension
fd -e sh -x shellcheck {} # Find and executerg - Recursive Grep
bash
rg 'DATABASE_URL' # Basic search
rg 'TODO' -t python # Search specific file types
rg -C 3 'error' # Search with contextjq - JSON处理
bash
jq . data.json # 格式化输出
jq -r '.key.subkey' data.json # 提取值
jq '.items[] | select(.status == "active")' # 过滤yq - YAML处理
bash
yq '.services.web.image' docker-compose.yml # 读取值
yq -i '.version = "2.1.0"' config.yml # 原地更新
yq -o json config.yml # 转换为JSONfd - 快速文件查找
bash
fd 'pattern' # 按模式查找
fd -e md # 按扩展名查找
fd -e sh -x shellcheck {} # 查找并执行检查rg - 递归文本搜索
bash
rg 'DATABASE_URL' # 基础搜索
rg 'TODO' -t python # 搜索特定文件类型
rg -C 3 'error' # 带上下文搜索Best Practices
最佳实践
Script Development Workflow
- Requirements Analysis: Understand automation need and target platforms
- Tool Selection: Choose appropriate tools for the task
- Prototype Development: Create initial script with core functionality
- Error Handling: Add robust error handling and edge case management
- Cross-Platform Testing: Verify script works on all target systems
- Performance Optimization: Profile and optimize for efficiency
- Documentation: Add clear usage instructions and inline comments
Critical Guidelines
- Always use shellcheck for linting
- Set strict mode:
set -euo pipefail - Quote all variables:
"${var}" - Use functions for reusable code
- Implement proper cleanup with trap
- Provide helpful error messages
- Include --help and --version options
- Use meaningful variable names
- Comment complex logic
- Test with different shells when targeting POSIX
脚本开发工作流
- 需求分析: 明确自动化需求与目标平台
- 工具选择: 为任务选择合适的工具
- 原型开发: 编写具备核心功能的初始脚本
- 错误处理: 添加健壮的错误处理与边缘情况管理
- 跨平台测试: 验证脚本在所有目标系统上均可运行
- 性能优化: 分析并优化以提升效率
- 文档编写: 添加清晰的使用说明与行内注释
关键准则
- 始终使用shellcheck进行代码检查
- 设置严格模式:
set -euo pipefail - 为所有变量添加引号:
"${var}" - 使用函数实现代码复用
- 使用trap实现正确的清理操作
- 提供有用的错误提示信息
- 包含--help和--version选项
- 使用有意义的变量名
- 为复杂逻辑添加注释
- 若目标为POSIX标准,需在不同Shell中测试
Common Patterns
常见模式
Robust Script Template
bash
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
trap 'echo "Error on line $LINENO"' ERR
trap cleanup EXIT
cleanup() {
rm -f "$TEMP_FILE" 2>/dev/null || true
}
main() {
parse_args "$@"
validate_environment
execute_task
}
main "$@"Cross-Platform Detection
bash
detect_os() {
case "$OSTYPE" in
linux*) OS="linux" ;;
darwin*) OS="macos" ;;
msys*) OS="windows" ;;
*) OS="unknown" ;;
esac
}For detailed command-line tools reference, advanced automation examples, and troubleshooting guidance, see REFERENCE.md.
健壮脚本模板
bash
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
trap 'echo "第$LINENO行出错"' ERR
trap cleanup EXIT
cleanup() {
rm -f "$TEMP_FILE" 2>/dev/null || true
}
main() {
parse_args "$@"
validate_environment
execute_task
}
main "$@"跨平台检测
bash
detect_os() {
case "$OSTYPE" in
linux*) OS="linux" ;; # Linux系统
darwin*) OS="macos" ;; # macOS系统
msys*) OS="windows" ;; # Windows系统
*) OS="unknown" ;; # 未知系统
esac
}如需详细的命令行工具参考、高级自动化示例以及故障排除指南,请查看REFERENCE.md。