テーマ: 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、1行1オブジェクトには -c、値を安全に渡すには --arg と --argjson、null や false を終了コードにするには -e、構造を変えるには 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