主题: shell
-
find and xargs exit codes: find's coarse 0/>0 versus xargs's 123-127 ladder
find only promises 0 for full success and any value above 0 for an error, deliberately without finer detail, while xargs defines a specific ladder for how the invoked command failed: 123 for an ordinary non-zero exit, 124 for exit 255, 125 for killed-by-signal, 126 for not-invocable, and 127 for not-found. Piping find into xargs means checking two different exit-code philosophies in one pipeline.
-
zsh as the macOS default shell: what breaks in a script written for bash
macOS has used zsh as the default login and interactive shell since macOS 10.15; a script that assumes bash's word-splitting, zero-based arrays or forgiving globbing can behave differently or fail outright under an agent's interactive zsh session on a Mac, even before /bin/bash's own age is considered.
-
grep, diff, cmp and test/[: exit code 1 means not-found, not failure
grep, diff, cmp and the test/[ builtin all use the same three-way scale: 0 for a positive result, 1 for a negative result that is not an error, and 2 (or >1) for real trouble. Reading 1 as a script failure is the most common misreading an agent makes with these tools.
-
用 grep 和 ripgrep 搜索代码:递归、过滤器与退出码
GNU grep 用 -r 递归搜索,用 --include 和 --exclude-dir 缩小范围,并分别以 0、1 或 2 表示有匹配、无匹配或出错;ripgrep 默认完成同类搜索,同时跳过 Git 忽略的文件、隐藏文件和二进制文件。在相信空结果之前,先了解每种工具会跳过什么。
-
Locale and date pitfalls in coreutils: LC_ALL, character ranges and GNU versus BSD date
The locale silently changes sort order, what [a-z] matches, decimal separators and the default date format; scripts should set LC_ALL=C (or C.UTF-8) and TZ explicitly, request explicit date formats, and avoid GNU-only date -d or BSD-only date -v unless the platform is known.
-
sort, uniq and comm: set operations on text that depend on sorted input
uniq only collapses adjacent duplicates, comm requires both inputs sorted in the same collation, and sort's order follows LC_COLLATE; run such pipelines under LC_ALL=C, pick the right key options (-n, -h, -V, -k, -t) and use comm -12 and comm -23 for intersections and differences.
-
"exec format error" and "cannot execute binary file": architecture and shebang mismatches
The kernel's ENOEXEC error surfaces as 'exec format error' or 'cannot execute binary file: Exec format error' when the CPU architecture does not match or a script lacks a usable shebang; CRLF line endings in the shebang cause a related 'bad interpreter' or 'required file not found' failure. The article shows how to tell them apart with file(1) and fix each without guessing.
-
find and xargs without surprises: null-separated names, -exec + and dry runs
File names may contain spaces and newlines, so pass them from find to other programs with -print0 and xargs -0 (or find -exec ... {} +), test the selection with -print before adding -delete, and use xargs -r so that an empty list runs nothing.
-
使用 jq 在命令行处理 JSON
jq 读取 JSON、应用过滤器并打印结果:用 -r 输出纯字符串,用 -c 每行输出一个对象,用 --arg 和 --argjson 安全传入值,用 -e 将 null 或 false 转为退出码,用 select、map 和 to_entries 重塑结构;绝不通过字符串拼接组装过滤器。
-
Writing shell scripts that fail safely
Use set -euo pipefail, quote every expansion, prefer [[ ]] and arrays, check tools with ShellCheck, and avoid parsing ls; a script that stops on the first error is easier to trust than one that continues.
-
会自我验证的首日设置脚本:bootstrap、doctor 与冒烟测试
克隆后的一条命令,要么生成可用环境,要么给出缺少什么以及如何修复的精确清单:只读 doctor 脚本检查每项要求,幂等 bootstrap 仅安装检查报告缺少的部分,冒烟测试决定退出状态;CI 从干净机器运行它,避免脚本在新人之间的空档期失效。
-
"Argument list too long" (E2BIG) and the ARG_MAX limit
A shell command that expands to too many arguments fails with 'Argument list too long', the E2BIG error from execve(2) hitting the kernel's ARG_MAX ceiling. The article shows how to measure the limit and replace a failing glob with xargs or find's -exec ... + batching instead of trying to quote around it.
-
sed and awk for line-oriented edits: substitution, fields and in-place changes
sed applies editing commands, chiefly s/regexp/replacement/, to each line; awk splits lines into fields and runs pattern-action pairs. Use sed for substitutions and deletions, awk for column work and small aggregations, and handle -i, -n and GNU-versus-BSD differences deliberately.
机器可读: JSON