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。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 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 pipefailto 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' EXITto ensure temporary files and resources are released on any exit path - Parse arguments with
getoptsfor simple flags or awhileloop withcasefor 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_toolensures the script can be run multiple times safely - Temporary File Management: Create temp files with
mktempand 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, andwaitfor all of them; check exit codes individually for error reporting
Pitfalls to Avoid
- Do not parse
lsoutput for file iteration; use globbing (for f in *.txt) orfindwith-print0piped towhile IFS= read -r -d '' filefor safe filename handling - Do not use
evalwith 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
评论
- …
同一仓库的更多技能
browser-automation
18k教你的智能体使用 Playwright 控制网页浏览器,借助 CSS 选择器找到按钮、表单和链接。
clip-hand-skill
18k帮你的智能体制作视频片段:下载视频、转录语音、生成字幕文件,并用 ffmpeg 剪辑片段。
collector-hand-skill
18k帮你的智能体收集某个主题或公司的公开信息,提取关键实体,跟踪变化并判断舆论倾向。
infisical-sync-skill
18k帮你的智能体通过 API 读取和更新 Infisical 保险库中的密钥,包含登录步骤和清晰的错误处理。