#!/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: indexed_array_create_read - literal and index assignment, read by index
my_array=(A B "C" D)
idx_arr[0]=value0
idx_arr[1]=value1
idx_arr[2]=value2
add_case \
"indexed_array_create_read" \
"验证括号字面量与下标单独赋值两种索引数组定义及按 0 基下标读取" \
"m=A,B,C,D;i=value0,value1,value2" \
"m=${my_array[0]},${my_array[1]},${my_array[2]},${my_array[3]};i=${idx_arr[0]},${idx_arr[1]},${idx_arr[2]}"
# Case 2: associative_array_declare_A - inline and incremental declare -A associative arrays
declare -A site_inline=(["google"]="www.google.com" ["portal"]="www.example.org" ["shop"]="shop.example.org")
declare -A site
site["google"]="www.google.com"
site["portal"]="www.example.org"
site["shop"]="shop.example.org"
site["portal"]="docs.example.org"
add_case \
"associative_array_declare_A" \
"验证 declare -A 关联数组内联初始化、逐键赋值、按键读取及唯一键覆盖更新" \
"inline=www.example.org;updated=docs.example.org;google=www.google.com" \
"inline=${site_inline[portal]};updated=${site[portal]};google=${site[google]}"
# Case 3: all_elements_and_keys - ${arr[*]}, ${arr[@]}, ${!site[@]}
my_array2[0]=A
my_array2[1]=B
my_array2[2]=C
my_array2[3]=D
vals_star="${my_array2[*]}"
vals_at="${my_array2[@]}"
sorted_vals=$(printf '%s\n' "${site_inline[@]}" | sort | paste -sd, -)
sorted_keys=$(printf '%s\n' "${!site_inline[@]}" | sort | paste -sd, -)
add_case \
"all_elements_and_keys" \
"验证 * 与 @ 获取数组全部元素,以及 ! 前缀获取关联数组全部键" \
"star=A B C D;at=A B C D;svals=shop.example.org,www.example.org,www.google.com;skeys=google,portal,shop" \
"star=${vals_star};at=${vals_at};svals=${sorted_vals};skeys=${sorted_keys}"
# Case 4: array_length_and_elem_len - ${#arr[*]}, ${#arr[@]}, element string length
cnt_star=${#my_array2[*]}
cnt_at=${#my_array2[@]}
cnt_assoc=${#site_inline[@]}
len_google=${#site_inline[google]}
add_case \
"array_length_and_elem_len" \
"验证 \${#array[*]}、\${#array[@]} 获取数组长度及单个元素字符串长度" \
"cnt_star=4;cnt_at=4;cnt_assoc=3;len_google=14" \
"cnt_star=${cnt_star};cnt_at=${cnt_at};cnt_assoc=${cnt_assoc};len_google=${len_google}"
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-array-lab/explore-shell-array.sh
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-shell-array.sh,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(debian:bookworm-slim,GNU bash 5.2.15,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联32章) · 含当前章节或实例 · mbskycom202612a1487.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1501.zip