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 從公開儲存庫中收錄這些內容。

檢舉或申請下架