Bash Defensive Patterns

作者 wshobson46891e7e60da无许可证收录于 2026年10月8日更新于 2026年10月8日

Master defensive Bash programming techniques for production-grade scripts. Use when writing robust shell scripts, CI/CD pipelines, or system utilities requiring fault tolerance and safety.

AI 生成的概览

提供编写健壮、容错 Bash 脚本的指导,涵盖防御性编程与错误处理。

功能
该技能提供编写生产级 Bash 脚本的最佳实践指导,采用防御性编程技术。内容涵盖严格模式、变量引用、错误捕获、输入校验、结构化日志、试运行支持、安全临时文件以及幂等性。详细模式与示例保存在单独的参考文件中,当摘要不够用时可以查阅。
适用场景
适用于编写生产自动化脚本、CI/CD 流水线脚本、系统管理工具或需要安全处理边界情况的部署自动化。也适合可维护的 shell 脚本库以及需要跨平台可靠性的脚本。
运行要求
不附带脚本,仅为说明文档。阅读随附的参考文件需要文件访问权限,但不需要任何软件包、运行时、凭据或网络访问。

Bash Defensive Patterns

Comprehensive guidance for writing production-ready Bash scripts using defensive programming techniques, error handling, and safety best practices to prevent common pitfalls and ensure reliability.

When to Use This Skill

  • Writing production automation scripts
  • Building CI/CD pipeline scripts
  • Creating system administration utilities
  • Developing error-resilient deployment automation
  • Writing scripts that must handle edge cases safely
  • Building maintainable shell script libraries
  • Implementing comprehensive logging and monitoring
  • Creating scripts that must work across different platforms

Detailed patterns and worked examples

Detailed pattern documentation lives in references/details.md. Read that file when the navigation tier above is insufficient.

Best Practices Summary

  1. Always use strict mode - set -Eeuo pipefail
  2. Quote all variables - "$variable" prevents word splitting
  3. Use [[]] conditionals - More robust than [ ]
  4. Implement error trapping - Catch and handle errors gracefully
  5. Validate all inputs - Check file existence, permissions, formats
  6. Use functions for reusability - Prefix with meaningful names
  7. Implement structured logging - Include timestamps and levels
  8. Support dry-run mode - Allow users to preview changes
  9. Handle temporary files safely - Use mktemp, cleanup with trap
  10. Design for idempotency - Scripts should be safe to rerun
  11. Document requirements - List dependencies and minimum versions
  12. Test error paths - Ensure error handling works correctly
  13. Use command -v - Safer than which for checking executables
  14. Prefer printf over echo - More predictable across systems

来源与署名

来源:wshobson/agents位于plugins/shell-scripting/skills/bash-defensive-patterns提交46891e7

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架