#!/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: if_elif_else_and_test - [ ], (( )), test, one-liner, empty else syntax error check
a=10; b=20
if [ "$a" -eq "$b" ]; then r_br="eq"
elif [ "$a" -gt "$b" ]; then r_br="gt"
elif [ "$a" -lt "$b" ]; then r_br="lt"
else r_br="other"
fi
if (( a == b )); then r_ar="eq"
elif (( a > b )); then r_ar="gt"
elif (( a < b )); then r_ar="lt"
else r_ar="other"
fi
num1=$((2 * 3)); num2=$((1 + 5))
r_test=$(if test "$num1" -eq "$num2"; then echo "eq"; else echo "ne"; fi)
r_one=$(if [ "$a" -lt "$b" ]; then echo "true"; fi)
set +e
bash -n -c 'if true; then echo ok; else fi' >/dev/null 2>&1
rc_empty_else=$?
set -e
add_case \
"if_elif_else_and_test" \
"验证 if/elif/else、(( ))、test、单行 if,以及空 else 分支触发语法错误(非零退出)" \
"br=lt;ar=lt;test=eq;one=true;empty_else_err=1" \
"br=${r_br};ar=${r_ar};test=${r_test};one=${r_one};empty_else_err=$((rc_empty_else != 0 ? 1 : 0))"
# Case 2: for_loop_lists_and_positional - numeric list, word list, one-liner, omitted in
nums=""
for loop in 1 2 3 4 5; do nums+="${loop}"; done
words=""
for str in This is a string; do words+="${str}/"; done
iter_pos() {
local out=""
for item; do out+="${item},"; done
printf '%s' "$out"
}
pos_res=$(iter_pos alpha beta gamma)
add_case \
"for_loop_lists_and_positional" \
"验证 for 遍历数字列表、单词列表及省略 in 子句时默认遍历位置参数" \
"nums=12345;words=This/is/a/string/;pos=alpha,beta,gamma," \
"nums=${nums};words=${words};pos=${pos_res}"
# Case 3: while_and_until_loops - while (( )), let, while read EOF, until 0..9
int=1; w_nums=""
while (( int <= 5 )); do
w_nums+="${int}"
let "int++"
done
read_out=""
while read -r site; do
read_out+="[${site}]"
done <<'EOF'
Linux
Bash
EOF
u=0; u_nums=""
until [ ! "$u" -lt 10 ]; do
u_nums+="${u}"
u=$(expr "$u" + 1)
done
add_case \
"while_and_until_loops" \
"验证 while (( )) 配合 let 计数、while read 读至 EOF 以及 until 输出 0~9" \
"w=12345;read=[Linux][Bash];u=0123456789" \
"w=${w_nums};read=${read_out};u=${u_nums}"
# Case 4: case_esac_patterns - numeric 1..4/*, string matching, multi-pattern a|b
match_num() {
case "$1" in
1) echo "one" ;;
2) echo "two" ;;
3) echo "three" ;;
4) echo "four" ;;
*) echo "other" ;;
esac
}
match_site() {
case "$1" in
"portal") echo "Portal" ;;
"google") echo "Google" ;;
"shop") echo "Shop" ;;
*) echo "Unknown" ;;
esac
}
add_case \
"case_esac_patterns" \
"验证 case...esac 多分支数字匹配、默认通配 * 分支及字符串分支匹配" \
"n3=three;n9=other;s=Portal" \
"n3=$(match_num 3);n9=$(match_num 9);s=$(match_site portal)"
# Case 5: break_and_continue_control - break exits infinite loop; continue skips trailing statement
brk_log=""
while read -r n; do
case "$n" in
1|2|3|4|5) brk_log+="ok:${n};" ;;
*) brk_log+="end:${n};"; break ;;
esac
done <<'EOF'
3
7
2
EOF
cont_log=""
while read -r n; do
case "$n" in
1|2|3|4|5) cont_log+="ok:${n};" ;;
*)
cont_log+="skip:${n};"
continue
cont_log+="UNREACHABLE;"
;;
esac
done <<'EOF'
3
7
2
EOF
add_case \
"break_and_continue_control" \
"验证 break 立即终止循环(不再处理后续 2),continue 跳过本轮后续语句但继续处理后续输入" \
"brk=ok:3;end:7;;cont=ok:3;skip:7;ok:2;" \
"brk=${brk_log};cont=${cont_log}"
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-process-control-lab/explore-shell-process-control.sh
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-shell-process-control.sh,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(debian:bookworm-slim,GNU bash 5.2.15,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联32章) · 含当前章节或实例 · mbskycom202612a1487.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1510.zip