agentleFS
28672032962867203296/hermes-agent/skills/software-development/shell-scripting/SKILL.md

shell-scripting

Skill0 starsChanged 3 months agoMITView on GitHub

Shell脚本最佳实践:错误处理、参数解析、纯bash技巧、模板.

  • Deletes or force-pushes

12 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use ci-cd, testing and 1 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • JohnJBorenSkill

    ci-cd

    CI/CD pipeline design, GitHub Actions, testing automation, and deployment strategies

    Also in 3 projects that use this

  • NVIDIASkill

    testing

    Run Python and Rust tests for physicsnemo-curator using uv + pytest with coverage reporting via pytest-cov, and cargo-nextest for Rust tests. Includes benchmark workflows with pytest-benchmark and criterion.

    Also in 3 projects that use this

  • GoogleSkill

    code-review

    Adversarial systems, concurrency, memory-model, and architecture code review protocol. Use this skill whenever asked to review code, PRs, diffs, or branches in Filament.

    Also in 4 projects that use this

The file

Files
SKILL.md

Shell 脚本最佳实践

借鉴 dylanaraps/pure-bash-bible (42k⭐) + Google Shell Style Guide。

脚本模板

#!/usr/bin/env bash
set -euo pipefail  # 严格模式:出错即停、未定义变量报错、管道错误

# 颜色
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m'

# 用法
usage() {
    echo "用法: $0 [选项] <参数>"
    echo "  -h  帮助"
    echo "  -v  详细输出"
    exit 1
}

# 参数解析
VERBOSE=false
while getopts "hv" opt; do
    case $opt in
        h) usage ;;
        v) VERBOSE=true ;;
        *) usage ;;
    esac
done
shift $((OPTIND-1))

# 主逻辑
main() {
    log "开始执行..."
    # ...
}

# 日志
log() { echo -e "${GREEN}[INFO]${NC} $(date '+%H:%M:%S') $*"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $*" >&2; }
err() { echo -e "${RED}[ERROR]${NC} $*" >&2; exit 1; }

main "$@"

纯 Bash 技巧(省掉外部命令)

# 字符串操作
s="hello world"
echo "${s^}"       # Hello world(首字母大写)
echo "${s^^}"      # HELLO WORLD(全大写)
echo "${s,}"       # hello world(首字母小写)
echo "${s,,}"      # hello world(全小写)
echo "${s/world/郑鑫}"  # hello 郑鑫(替换)

# 默认值
echo "${VAR:-默认值}"       # VAR为空时用默认值
echo "${VAR:=默认值}"       # VAR为空时赋值默认值
: "${VAR:?必须设置}"        # VAR为空时报错退出

# 数组
arr=(a b c)
echo "${arr[0]}"          # a
echo "${arr[@]}"          # a b c(所有元素)
echo "${#arr[@]}"         # 3(长度)
arr+=(d)                  # 追加

# 循环
for i in {1..10}; do echo "$i"; done
for f in *.txt; do echo "$f"; done

# 条件
[[ -f "$file" ]]   # 文件存在且是普通文件
[[ -d "$dir" ]]    # 目录存在
[[ -z "$str" ]]    # 字符串为空
[[ -n "$str" ]]    # 字符串非空
[[ "$a" == "$b" ]] # 字符串相等

# 数学运算
((count++))
((total += value))
echo $((10 + 5 * 3))  # 25

# 子串
echo "${s:0:5}"    # hello(位置0起取5个字符)
echo "${s#*.}"     # txt(去掉最短匹配前缀)
echo "${s##*.}"    # txt(去掉最长匹配前缀)
echo "${s%.*}"     # file(去掉最短匹配后缀)
echo "${s%%.*}"    # file(去掉最长匹配后缀)

严格模式详解

set -e  # 任何命令返回非0就退出
set -u  # 使用未定义变量就退出
set -o pipefail  # 管道中任何一个命令失败都算失败

# 如果你需要某个命令失败不退出:
command_that_may_fail || true

# 如果需要检查返回值:
if ! command; then
    echo "失败了"
fi

常见模式

# 确保在项目根目录
cd "$(dirname "$0")/.."

# 临时目录 + 自动清理
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT

# 检查依赖
for cmd in git curl jq; do
    if ! command -v "$cmd" >/dev/null; then
        err "需要 $cmd,请先安装"
    fi
done

# 确认操作
read -p "确认? (y/n) " -n 1 -r; echo
[[ $REPLY =~ ^[Yy]$ ]] || exit 0

# 逐行读文件
while IFS= read -r line; do
    echo "$line"
done < file.txt

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.