# shellkit > 写运维 shell 的起点:一个约束清晰的 Bash 库、一个脚手架生成器、一套 CLI 框架。基线 Bash 3.2+。 stdout 只给数据,日志一律 stderr;失败有固定退出码;同一条命令跑一百遍和跑一遍结果一样。 ## 开始 - [指南](https://yi-nology.github.io/shellkit/guide/index.html): 这套文档按「你现在要解决什么」组织,不按功能目录组织。 - [安装与升级](https://yi-nology.github.io/shellkit/guide/installation.html): 三种装法,选一种就行:一条命令、本地检出、内网离线。 - [三十秒上手](https://yi-nology.github.io/shellkit/guide/quickstart.html): 生成一个工程,看它长什么样,加一个自己的子命令。 - [五条硬约定](https://yi-nology.github.io/shellkit/guide/conventions.html): 这五条不是风格偏好,每一条都有具体后果。违反任意一条,脚本会在某个不起眼的地方错, - [Bash 3.2 兼容](https://yi-nology.github.io/shellkit/guide/bash32.html): 基线是 **Bash 3.2+**,不是 4.x 也不是 5.x。原因很具体:macOS 自带的 `/bin/bash` 就是 3.2.57, - [模块地图](https://yi-nology.github.io/shellkit/guide/modules.html): `lib/bootstrap.sh` 是唯一入口,一次性按**拓扑序**加载全部 27 个模块。你不用挑着 source, - [Cookbook:按任务查](https://yi-nology.github.io/shellkit/guide/cookbook.html): 每节都是「场景 → 可以抄的代码 → 为什么这么写」。代码块都是能跑的片段,不是伪代码。 - [CLI 框架](https://yi-nology.github.io/shellkit/guide/cli.html): `lib/core/cli.sh` 只做五件事:注册、分发、帮助生成、选项解析、退出码。 - [国际化](https://yi-nology.github.io/shellkit/guide/i18n.html): msgid 一律英文,译文放 `locale//LC_MESSAGES/shellkit.po`。 - [测试与门禁](https://yi-nology.github.io/shellkit/guide/testing.html): 这个仓库的测试有三层,都是为了同一件事:**证明它真的跑了**,而不是「应该能跑」。 - [给 AI 用](https://yi-nology.github.io/shellkit/guide/ai.html): 这套约束本来就是写给机器看的:禁用语法、任务落点、交付自检,全部是可判定的规则, - [示例工程](https://yi-nology.github.io/shellkit/guide/examples.html): 两个能直接跑的完整工程。演示的是**装配**,不是把库再实现一遍。 - [shellkit man:把手册页生成出来](https://yi-nology.github.io/shellkit/guide/man.html): `shellkit man` 把 shellkit 自己的 man page 生成出来:第 1 节是命令总览,每个库模块一页第 3 节。 - [把 shellkit 接进既有项目](https://yi-nology.github.io/shellkit/guide/migrating.html): 不要求你先重写脚本。三种接入姿势从"零承诺"到"全套",按你能接受的风险选。 - [兼容性与弃用政策](https://yi-nology.github.io/shellkit/guide/compatibility.html): 状态:自 **v0.27.0** 起生效,v1.0.0 发布时冻结。本文回答三件事: - [发版](https://yi-nology.github.io/shellkit/guide/releasing.html): 发版不是「改个版本号再打个 tag」:版本号散在五个文件、九处引用里,而它们全是手抄的; - [参与开发](https://yi-nology.github.io/shellkit/guide/contributing.html): 全部步骤全绿才算过(清单的唯一真相源是 `Makefile` 的 `check` 目标)。单跑某一步: - [故障排查](https://yi-nology.github.io/shellkit/guide/troubleshooting.html): 先看退出码,再看症状表。**退出码就是接口**,它比日志更可靠 —— 日志可能被 `--no-color` - [FAQ](https://yi-nology.github.io/shellkit/guide/faq.html): 因为 macOS 自带的 `/bin/bash` 是 3.2.57,而且很多机器上装不了 homebrew。基线定在 3.2, - [变更记录](https://yi-nology.github.io/shellkit/guide/changelog.html): 本文件由 `tools/release.sh` 在发版时自动追加,已发布的小节不要手改。 - [机器可读输出](https://yi-nology.github.io/shellkit/guide/json.html): 人读的是散行,脚本读的是字段。shellkit 的机器可读输出集中写在这一页 —— - [POSIX 子集(设备侧)](https://yi-nology.github.io/shellkit/guide/posix.html): shellkit 的基线是 **Bash 3.2+**。但设备侧脚本跑在别的地方:NAS 的 start/stop、 - [安全边界规范](https://yi-nology.github.io/shellkit/guide/security.html): 四条边界:**路径 / 权限 / 敏感参数 / 执行隔离**。 ## API 参考 每个公开函数的签名与退出码,由源码注释生成,不会和代码漂移。 - [API 参考](https://yi-nology.github.io/shellkit/reference/index.html) - [archive](https://yi-nology.github.io/shellkit/reference/archive.html): archive_list ARCHIVE —— 打印归档里的成员清单(相对路径,每行一个) 只读不解:不写盘、不建目录。用来「先看看包里有什么」再决定解不解。 - [cli](https://yi-nology.github.io/shellkit/reference/cli.html): cli_group TITLE —— 后续注册的命令归入该分组 - [config](https://yi-nology.github.io/shellkit/reference/config.html): conf_init FILE [--format env|dotenv] —— 载入配置文件;文件不存在不报错(当成空配置) --format 决定文件怎么解析:auto(默认,老行为 + 容忍 dotenv 写法)、 env(严格 KEY=VALUE)、dotenv(完整 dotenv 语义)。 严格格式(env / dotenv)下不合规的行会在载入时报警并跳过。 - [error](https://yi-nology.github.io/shellkit/reference/error.html): die CODE MSG... —— CODE 必须是 0-255;不合法按用法错误退出 2,而不是让 exit 截成 255 - [fs](https://yi-nology.github.io/shellkit/reference/fs.html): fs_ensure_dir DIR [MODE] —— 幂等建目录;给了 MODE 就 chmod - [git](https://yi-nology.github.io/shellkit/reference/git.html): git_default_branch REPO —— 解析默认分支 顺序:origin/HEAD 的符号引用(正常 clone 都有)→ 本地已有的 origin/main 或 origin/master → 远端只有一个分支时就是它 → 多个远端分支且认不出 ⇒ 报歧义。 认不出就报错,不猜 —— 猜错了的代价是「拉错分支」。 只看 origin 这一个远端:默认分支是「这个仓的」概念,多远端时猜哪个远端 同样是猜 —— 与其猜,不如让调用方显式指定分支名。 - [http](https://yi-nology.github.io/shellkit/reference/http.html): METHOD 不区分大小写;请求完成返回 0,状态码不算错误。 - [i18n](https://yi-nology.github.io/shellkit/reference/i18n.html) - [lock](https://yi-nology.github.io/shellkit/reference/lock.html): lock_acquire NAME [WAIT_SEC] —— 0 成功;SK_PRECOND 被占用/超时;SK_USAGE 名字非法 - [log](https://yi-nology.github.io/shellkit/reference/log.html): log_info MSG... —— 普通信息 - [man](https://yi-nology.github.io/shellkit/reference/man.html): man_modules —— 输出模块名,一行一个 - [net](https://yi-nology.github.io/shellkit/reference/net.html): net_sha256 FILE —— 文件的 sha256;两种工具都没有时返回 1 - [os](https://yi-nology.github.io/shellkit/reference/os.html): os_name —— linux | darwin | other - [proc](https://yi-nology.github.io/shellkit/reference/proc.html): proc_start NAME COMMAND... —— 后台拉起一个进程组 脱离终端:nohup 忽略 SIGHUP + 标准输入接 /dev/null + 作业控制让它自带进程组。 pid 文件:$DIR/NAME.pid(原子写)。日志:$DIR/NAME.log(**追加**,不截断 —— 截断会把上次的现场抹掉,出事时没得查)。 - [profile](https://yi-nology.github.io/shellkit/reference/profile.html): profile_write FILE —— 创建/清空 profile 文件 - [prompt](https://yi-nology.github.io/shellkit/reference/prompt.html): ask VAR PROMPT [DEFAULT] [TIMEOUT] —— 问一个问题,把答案写进 VAR;超时或 EOF 回退默认值 VAR 不能用库内部出参链路上的局部变量名(见 _sk_check_out_name),否则会静默失效。 - [registry](https://yi-nology.github.io/shellkit/reference/registry.html): 与 registry_scan 的区别:registry_scan 扫目录,registry_add 登记单份文件。 - [retry](https://yi-nology.github.io/shellkit/reference/retry.html): retry ATTEMPTS CMD... —— 失败退避重试;用法错与校验失败不重试 - [run](https://yi-nology.github.io/shellkit/reference/run.html): run [--always] [--no-die] [--secret-arg N] [--redact-value V] [--] CMD... —— 执行命令,失败即 die;--no-die 只返回码 命令行会进日志,敏感参数会被脱敏(键名像敏感字段、请求头、短选项 -u 等)。 启发式总会漏,所以还留了两个显式声明: --secret-arg N 第 N 个参数(命令名不算)必脱敏,可以写多次 --redact-value V 值等于 V 的地方一律脱敏(令牌夹在参数中间也遮得到) 环境变量 SHELLKIT_SECRET_ARGS="2,3" 等价于 --secret-arg 2 --secret-arg 3, 对所有 run 生效 —— CI 里统一声明最省事。 - [scaffold](https://yi-nology.github.io/shellkit/reference/scaffold.html): scaffold_vars_set KEY VALUE —— 设一个模板变量 - [status](https://yi-nology.github.io/shellkit/reference/status.html): status_is_machine —— 机器模式返回 0,人类模式返回 1 - [str](https://yi-nology.github.io/shellkit/reference/str.html): str_trim VAR —— 去掉首尾空白,原地改写该变量 - [svc](https://yi-nology.github.io/shellkit/reference/svc.html): svc_platform —— 当前平台的服务管理器 可用 SK_SVC_PLATFORM 覆盖(测试与特殊环境用)。 - [trap](https://yi-nology.github.io/shellkit/reference/trap.html): trap_push CMD... —— 注册一个清理动作;后注册的先执行 - [validate](https://yi-nology.github.io/shellkit/reference/validate.html): validate_not_empty VALUE —— 非空校验;失败原因写进 SK_VALIDATE_ERR - [vendor](https://yi-nology.github.io/shellkit/reference/vendor.html): vendor_dir_rel PROJECT_DIR —— 库副本相对工程目录的路径(配置值原样) 默认是 vendor/shellkit;配了 SHELLKIT_VENDOR_DIR 或 .shellkit-vendor-dir 就是那个值。 - [ver](https://yi-nology.github.io/shellkit/reference/ver.html): ver_cmp A B —— 比较两个版本号 ## 英文文档 - [Guide](https://yi-nology.github.io/shellkit/en/guide/index.html) - [API reference](https://yi-nology.github.io/shellkit/en/reference/index.html)