#!/usr/bin/env bash
set -euo pipefail
export LC_ALL=C.UTF-8
WORKDIR=$(mktemp -d)
trap 'rm -rf "$WORKDIR"' EXIT
VERSION="Debian bookworm-slim GNU bash 5.2.15 (--network none)"
CASES=()
json_str() {
local s="$1"
s=${s//\\/\\\\}
s=${s//\"/\\\"}
s=${s//$'\n'/\\n}
s=${s//$'\r'/\\r}
s=${s//$'\t'/\\t}
printf '"%s"' "$s"
}
add_case() {
local name="$1" summary="$2" expected="$3" actual="$4"
local passed="false"
if [[ "$expected" == "$actual" ]]; then
passed="true"
fi
CASES+=("{\"name\":$(json_str "$name"),\"summary\":$(json_str "$summary"),\"expected\":$(json_str "$expected"),\"actual\":$(json_str "$actual"),\"passed\":$passed}")
}
emit_json() {
printf '{"version":%s,"cases":[' "$(json_str "$VERSION")"
local i
for ((i = 0; i < ${#CASES[@]}; i++)); do
if ((i > 0)); then printf ','; fi
printf '%s' "${CASES[$i]}"
done
printf ']}\n'
}
# Case 1: stdout_overwrite_and_append - > overwrites file; >> appends to end
echo "initial_user_1" > "$WORKDIR/users"
echo "initial_user_2" >> "$WORKDIR/users"
echo "BlueSky Linux Tutorial" > "$WORKDIR/users"
after_overwrite=$(wc -l < "$WORKDIR/users" | tr -d ' ')
echo "BlueSky Linux Tutorial" >> "$WORKDIR/users"
after_append=$(wc -l < "$WORKDIR/users" | tr -d ' ')
add_case \
"stdout_overwrite_and_append" \
"验证 > 覆盖写入文件与 >> 追加到文件末尾的精确行数变化" \
"overwrite=1;append=2" \
"overwrite=${after_overwrite};append=${after_append}"
# Case 2: stdin_redirect_and_combined - wc -l file vs wc -l < file, and < infile > outfile
wc_with_name=$(cd "$WORKDIR" && wc -l users | awk '{print $1":"$2}')
wc_stdin_only=$(cd "$WORKDIR" && wc -l < users | tr -d ' ')
printf 'hello linux\n' > "$WORKDIR/infile"
tr 'a-z' 'A-Z' < "$WORKDIR/infile" > "$WORKDIR/outfile"
out_upper=$(cat "$WORKDIR/outfile")
add_case \
"stdin_redirect_and_combined" \
"验证 wc -l users 输出文件名而 wc -l < users 仅输出行数,以及 < infile > outfile 双向重定向" \
"with_name=2:users;stdin_only=2;upper=HELLO LINUX" \
"with_name=${wc_with_name};stdin_only=${wc_stdin_only};upper=${out_upper}"
# Case 3: stderr_and_fd_merging - 2>, 2>>, > file 2>&1, >> file 2>&1, custom fd n>&m and n<&m
emit_both() {
echo "OUT_LINE"
echo "ERR_LINE" >&2
}
emit_both >/dev/null 2> "$WORKDIR/err.log"
emit_both >/dev/null 2>> "$WORKDIR/err.log"
err_lines=$(wc -l < "$WORKDIR/err.log" | tr -d ' ')
emit_both > "$WORKDIR/both.log" 2>&1
both_sorted=$(sort "$WORKDIR/both.log" | paste -sd, -)
# Custom FD 3 write & read (n > file, n >& m, n <& m)
exec 3> "$WORKDIR/fd3.txt"
echo "FD3_DATA" >&3
exec 3>&-
exec 4< "$WORKDIR/fd3.txt"
read -r fd4_val <&4
exec 4<&-
add_case \
"stderr_and_fd_merging" \
"验证 2>/2>> 捕获标准错误、> file 2>&1 合并标准输出与错误,以及自定义文件描述符 n>&m / n<&m" \
"err_lines=2;both=ERR_LINE,OUT_LINE;fd4=FD3_DATA" \
"err_lines=${err_lines};both=${both_sorted};fd4=${fd4_val}"
# Case 4: heredoc_and_dev_null - wc -l << EOF, cat << EOF, > /dev/null 2>&1, < /dev/null
hd_lines=$(wc -l <<EOF | tr -d ' '
Welcome
Linux Shell
Here Document
EOF
)
hd_text=$(cat <<EOF | paste -sd'|' -
LineA
LineB
EOF
)
null_capture=$(emit_both > /dev/null 2>&1)
null_read_bytes=$(wc -c < /dev/null | tr -d ' ')
add_case \
"heredoc_and_dev_null" \
"验证 Here Document 行数统计与输出、> /dev/null 2>&1 静默丢弃双流及读取 /dev/null 返回 0 字节" \
"hd_lines=3;hd_text=LineA|LineB;null_cap=;null_read=0" \
"hd_lines=${hd_lines};hd_text=${hd_text};null_cap=${null_capture};null_read=${null_read_bytes}"
emit_json
本例需在 本地离线 Docker 容器(debian:bookworm-slim,GNU bash 5.2.15,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地离线 Docker 容器(debian:bookworm-slim,GNU bash 5.2.15,--network none)。
入口文件:projects/linux-shell-io-redirections-lab/explore-shell-io-redirections.sh
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-shell-io-redirections.sh,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(debian:bookworm-slim,GNU bash 5.2.15,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联32章) · 含当前章节或实例 · mbskycom202612a1487.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1507.zip