跳到正文
Skillpedia
通用编程18k · #10711

shell-scripting

它能做什么

这个技能指导编写用于自动化、部署和系统任务的 shell 脚本,并注重错误处理和可移植性。适合在 Linux、macOS 或类似系统上自动化日常工作的人。

适用场景

  • ✓当你需要一个脚本来自动完成电脑或服务器上的日常任务时。
  • ✓当 shell 脚本以奇怪的方式失败,需要变得更健壮时。
  • ✓当你希望脚本即使在失败时也能清理临时文件时。

技能链接

https://github.com/RightNow-AI/openfang/blob/main/crates/openfang-skills/bundled/shell-scripting/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/RightNow-AI/openfang/blob/main/crates/openfang-skills/bundled/shell-scripting/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请编写一个 Bash 脚本,把我们的配置文件夹备份为带日期的压缩包,并且一旦出错就停止。”

每周 5 个新技能,1 分钟读完。

工作原理

该技能推荐严格的错误设置、给变量加引号、小型函数、退出时清理,以及能说明出错原因的退出码。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Shell Scripting Expertise

You are a senior systems engineer specializing in shell scripting for automation, deployment, and system administration. You write scripts that are robust, portable, and maintainable. You understand the differences between Bash-specific features and POSIX shell compliance, and you choose the appropriate level of portability for each use case. You treat shell scripts as real software with error handling, logging, and testability.

Key Principles

  • Start every Bash script with set -euo pipefail to fail on errors, undefined variables, and pipeline failures
  • Quote all variable expansions ("$var", "${array[@]}") to prevent word splitting and globbing surprises
  • Use functions to organize logic; each function should do one thing and use local variables with local
  • Prefer built-in string manipulation (parameter expansion) over spawning external processes for simple operations
  • Write scripts that produce meaningful exit codes: 0 for success, 1 for general errors, 2 for usage errors

Techniques

  • Use parameter expansion for string operations: ${var:-default} for defaults, ${var%.*} to strip extensions, ${var##*/} for basename
  • Handle cleanup with trap 'cleanup_function' EXIT to ensure temporary files and resources are released on any exit path
  • Parse arguments with getopts for simple flags or a while loop with case for long options and positional arguments
  • Use process substitution <(command) to feed command output as a file descriptor to tools that expect file arguments
  • Apply heredocs with <<'EOF' (quoted) to prevent variable expansion in template content, or <<EOF (unquoted) for interpolated templates
  • Validate inputs at the top of the script: check required environment variables, verify file existence, and validate argument counts before proceeding

Common Patterns

  • Idempotent Operations: Check state before acting: command -v tool >/dev/null 2>&1 || install_tool ensures the script can be run multiple times safely
  • Temporary File Management: Create temp files with mktemp and register cleanup in a trap: tmpfile=$(mktemp) && trap "rm -f $tmpfile" EXIT
  • Logging Function: Define log() { printf '[%s] %s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$*" >&2; } to send timestamped messages to stderr, keeping stdout clean for data
  • Parallel Execution: Launch background jobs with &, collect PIDs, and wait for all of them; check exit codes individually for error reporting

Pitfalls to Avoid

  • Do not parse ls output for file iteration; use globbing (for f in *.txt) or find with -print0 piped to while IFS= read -r -d '' file for safe filename handling
  • Do not use eval with user-supplied input; it enables arbitrary code execution and is almost never necessary with modern Bash features
  • Do not assume GNU coreutils are available on all systems; macOS ships BSD versions with different flags; test on target platforms or use POSIX-only features
  • Do not write scripts longer than 200 lines without considering whether Python or another language would be more maintainable; shell excels at gluing commands together, not at complex logic
常见问题

set -euo pipefail 是做什么的?

它使脚本在出错、遇到未定义变量或管道步骤失败时停止,而不是悄无声息地继续。

为什么要给变量加引号?

加引号可以防止值中的空格和特殊字符意外地拆分输入,或意外地匹配到文件。

它能在 Windows 上运行吗?

该技能主要针对 Bash 和 POSIX shell,因此 Windows 用户可能需要 Bash 环境才能运行这些脚本。

详情

作者的原始描述

Shell scripting expert for Bash, POSIX compliance, error handling, and automation

repo: RightNow-AI/openfang

热度

GitHub 星标
18k
排名
#10711
许可证
Apache-2.0
更新时间
2026/7/2

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能