#!/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: positional_args_0_to_3 - pass 3 arguments to executable script
cat <<'EOF' > "$WORKDIR/args.sh"
#!/bin/bash
printf "f=%s;1=%s;2=%s;3=%s;n=%s" "$0" "$1" "$2" "$3" "$#"
EOF
chmod +x "$WORKDIR/args.sh"
out1=$(cd "$WORKDIR" && ./args.sh 1 2 3)
add_case \
"positional_args_0_to_3" \
"向可执行脚本传递 1 2 3 三个参数并核对 \$0、\$1、\$2、\$3 与 \$#" \
"f=./args.sh;1=1;2=2;3=3;n=3" \
"$out1"
# Case 2: star_vs_at_quoted - "$*" yields 1 combined word while "$@" preserves each argument
cat <<'EOF' > "$WORKDIR/star_at.sh"
#!/bin/bash
star_cnt=0
star_items=""
for i in "$*"; do
star_cnt=$((star_cnt + 1))
star_items+="[${i}]"
done
at_cnt=0
at_items=""
for i in "$@"; do
at_cnt=$((at_cnt + 1))
at_items+="[${i}]"
done
printf "star=%d:%s;at=%d:%s" "$star_cnt" "$star_items" "$at_cnt" "$at_items"
EOF
chmod +x "$WORKDIR/star_at.sh"
out2=$(cd "$WORKDIR" && ./star_at.sh 1 2 3)
out2_sp=$(cd "$WORKDIR" && ./star_at.sh "a b" "c")
add_case \
"star_vs_at_quoted" \
"对比双引号中 \"\$*\" 与 \"\$@\" 在 1 2 3 及含空格参数下的遍历差异" \
"basic=star=1:[1 2 3];at=3:[1][2][3]|space=star=1:[a b c];at=2:[a b][c]" \
"basic=${out2}|space=${out2_sp}"
# Case 3: special_params_status_pids - $$, $!, $-, $?
pid_ok=$([[ "$$" =~ ^[0-9]+$ ]] && echo 1 || echo 0)
sleep 0.05 &
bg_pid=$!
bg_ok=$([[ "$bg_pid" =~ ^[0-9]+$ && "$bg_pid" -ne "$$" ]] && echo 1 || echo 0)
wait "$bg_pid"
wait_rc=$?
opts_nonempty=$([[ -n "$-" ]] && echo 1 || echo 0)
true
rc_true=$?
set +e
false
rc_false=$?
set -e
add_case \
"special_params_status_pids" \
"验证特殊参数 \$\$(当前 PID)、\$!(后台进程 PID)、\$-(Shell 选项)与 \$?(退出状态)" \
"pid_ok=1;bg_ok=1;wait_rc=0;opts=1;rc_true=0;rc_false=1" \
"pid_ok=${pid_ok};bg_ok=${bg_ok};wait_rc=${wait_rc};opts=${opts_nonempty};rc_true=${rc_true};rc_false=${rc_false}"
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-passing-arguments-lab/explore-shell-passing-arguments.sh
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-shell-passing-arguments.sh,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(debian:bookworm-slim,GNU bash 5.2.15,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联32章) · 含当前章节或实例 · mbskycom202612a1487.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1508.zip