本文へスキップ
Skillpedia
一般的なプログラミング41k · #6783

bash-defensive-patterns

できること

このガイドでは、より安全で信頼性の高い自動化スクリプト(Bash)の書き方を学べます。よくあるエラーを防ぎ、予期しない状況でもスクリプトが正しく動作するようにする手助けをします。

使う場面

  • ✓業務で重要な作業を自動化するスクリプトを作るとき。
  • ✓開発プロセス(CI/CD)の自動化ステップを構築するとき。
  • ✓高い信頼性が求められるシステムツールを開発するとき。

スキルのリンク

https://github.com/wshobson/agents/blob/main/plugins/shell-scripting/skills/bash-defensive-patterns/SKILL.md

コピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。

使い方

いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。

貼り付け用メッセージ

このスキルを使って手伝ってください:https://github.com/wshobson/agents/blob/main/plugins/shell-scripting/skills/bash-defensive-patterns/SKILL.md このリンクのSKILL.mdファイルを読んで、私の作業でその指示に従ってください。
こういうスキルを、AIが自分で見つけてくれたら?無料のSkillpedia MCPをインストール。1行で、作業の前に15,000+のスキルから探してくれます。1分でインストール →

AIへの頼み方の例

“ファイルが存在しなくても失敗しないBashスクリプトは、どうすれば書けますか?”

毎週5つの新しいスキル。1分で読めます。

仕組み

安全モードを有効にする(`set -euo pipefail`)、変数を常にクォートで囲む(`"$variable"`)、コマンドが正常に実行されたかを確認する、といったテクニックを使います。

技術的な内容(元のSKILL.md)

作者による元のテキスト(英語)をGitHubからそのまま表示しています。

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
よくある質問

「厳格モード(strict mode)」とは何ですか?

スクリプトをより厳密に動かすためのルールの集まり(`set -Eeuo pipefail`)です。そうでなければ見過ごされがちな一般的なエラーに対して、修正を強制します。

変数をクォートで囲むべきなのはなぜですか?

変数を`"$variable"`のようにクォートで囲むと、変数の値に含まれる空白や特殊文字をスクリプトが誤って解釈するのを防ぎ、エラーを回避できるからです。

「冪等性(idempotency)」とは何ですか?

冪等なスクリプトは、何度実行しても意図しない副作用を起こさずに済むスクリプトです。実行回数に関係なく、最終的な結果が同じになることを保証します。

詳細

作者による元の説明

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.

repo: wshobson/agents

人気

GitHubのスター
41k
ランキング
#6783
ライセンス
MIT
更新日
2026/10/15

このスキルは役に立ちますか?

0

ログイン して投票しましょう。

コメント

  • …

このリポジトリの他のスキル

似ているスキル