#!/usr/bin/env bash
set -euo pipefail
WORKDIR=$(mktemp -d)
trap 'rm -rf "$WORKDIR"' EXIT
VERSION="Debian bookworm-slim GNU bash ${BASH_VERSION%%(*} (--network none)"
# Case 1: file_types_and_ls_l_format
touch "$WORKDIR/reg.txt"
mkdir "$WORKDIR/sub_dir"
ln -s "$WORKDIR/reg.txt" "$WORKDIR/sym_link"
t_reg=$(stat -c '%A' "$WORKDIR/reg.txt" | cut -c1)
t_dir=$(stat -c '%A' "$WORKDIR/sub_dir" | cut -c1)
t_lnk=$(stat -c '%A' "$WORKDIR/sym_link" | cut -c1)
t_chr=$(stat -c '%A' /dev/null | cut -c1)
mode_len=$(stat -c '%A' "$WORKDIR/reg.txt" | wc -c | tr -d ' ')
# wc -c counts trailing newline, so 10 chars + newline = 11; use ${#...}
m_str=$(stat -c '%A' "$WORKDIR/reg.txt")
mode_len=${#m_str}
c1_actual="{\"regular\":\"${t_reg}\",\"dir\":\"${t_dir}\",\"symlink\":\"${t_lnk}\",\"chardev\":\"${t_chr}\",\"perm_string_len\":${mode_len}}"
c1_expected='{"regular":"-","dir":"d","symlink":"l","chardev":"c","perm_string_len":10}'
c1_passed=$([ "$c1_actual" = "$c1_expected" ] && echo true || echo false)
# Case 2: chown_and_chgrp_operations
mkdir -p "$WORKDIR/tree/child"
touch "$WORKDIR/tree/install.log" "$WORKDIR/tree/child/app.conf"
chgrp -R daemon "$WORKDIR/tree"
og_chgrp=$(stat -c '%U:%G' "$WORKDIR/tree/child/app.conf")
chown bin "$WORKDIR/tree/install.log"
og_chown_user=$(stat -c '%U:%G' "$WORKDIR/tree/install.log")
chown root:root "$WORKDIR/tree/install.log"
og_chown_both=$(stat -c '%U:%G' "$WORKDIR/tree/install.log")
c2_actual="{\"after_chgrp_R\":\"${og_chgrp}\",\"after_chown_bin\":\"${og_chown_user}\",\"after_chown_root_root\":\"${og_chown_both}\"}"
c2_expected='{"after_chgrp_R":"root:daemon","after_chown_bin":"bin:daemon","after_chown_root_root":"root:root"}'
c2_passed=$([ "$c2_actual" = "$c2_expected" ] && echo true || echo false)
# Case 3: chmod_numeric_octal_modes
touch "$WORKDIR/.bashrc_demo"
chmod 644 "$WORKDIR/.bashrc_demo"
m644=$(stat -c '%a %A' "$WORKDIR/.bashrc_demo")
chmod 777 "$WORKDIR/.bashrc_demo"
m777=$(stat -c '%a %A' "$WORKDIR/.bashrc_demo")
chmod 754 "$WORKDIR/.bashrc_demo"
m754=$(stat -c '%a %A' "$WORKDIR/.bashrc_demo")
chmod 770 "$WORKDIR/.bashrc_demo"
m770=$(stat -c '%a %A' "$WORKDIR/.bashrc_demo")
chmod -R 750 "$WORKDIR/tree"
m_rec=$(stat -c '%a' "$WORKDIR/tree/child/app.conf")
c3_actual="{\"m644\":\"${m644}\",\"m777\":\"${m777}\",\"m754\":\"${m754}\",\"m770\":\"${m770}\",\"recursive_child\":\"${m_rec}\"}"
c3_expected='{"m644":"644 -rw-r--r--","m777":"777 -rwxrwxrwx","m754":"754 -rwxr-xr--","m770":"770 -rwxrwx---","recursive_child":"750"}'
c3_passed=$([ "$c3_actual" = "$c3_expected" ] && echo true || echo false)
# Case 4: chmod_symbolic_modes
touch "$WORKDIR/test1"
chmod 644 "$WORKDIR/test1"
chmod u=rwx,g=rx,o=r "$WORKDIR/test1"
s_set=$(stat -c '%a %A' "$WORKDIR/test1")
chmod a-x "$WORKDIR/test1"
s_drop=$(stat -c '%a %A' "$WORKDIR/test1")
chmod g+w,o-r "$WORKDIR/test1"
s_delta=$(stat -c '%a %A' "$WORKDIR/test1")
c4_actual="{\"after_u_g_o_eq\":\"${s_set}\",\"after_a_minus_x\":\"${s_drop}\",\"after_g_plus_w_o_minus_r\":\"${s_delta}\"}"
c4_expected='{"after_u_g_o_eq":"754 -rwxr-xr--","after_a_minus_x":"644 -rw-r--r--","after_g_plus_w_o_minus_r":"660 -rw-rw----"}'
c4_passed=$([ "$c4_actual" = "$c4_expected" ] && echo true || echo false)
cat <<EOF
{
"version": "${VERSION}",
"cases": [
{
"name": "file_types_and_ls_l_format",
"summary": "实测 -/d/l/c 四类文件首字符标识及 10 位权限属性串长度",
"expected": ${c1_expected},
"actual": ${c1_actual},
"passed": ${c1_passed}
},
{
"name": "chown_and_chgrp_operations",
"summary": "实测 chgrp -R 递归改属组、chown 修改属主及 chown user:group 同时变更",
"expected": ${c2_expected},
"actual": ${c2_actual},
"passed": ${c2_passed}
},
{
"name": "chmod_numeric_octal_modes",
"summary": "实测 chmod 八进制 644/777/754/770 及 -R 递归权限变更结果",
"expected": ${c3_expected},
"actual": ${c3_actual},
"passed": ${c3_passed}
},
{
"name": "chmod_symbolic_modes",
"summary": "实测 chmod 符号模式 u=rwx,g=rx,o=r、a-x 与 g+w,o-r 权限演进",
"expected": ${c4_expected},
"actual": ${c4_actual},
"passed": ${c4_passed}
}
]
}
EOF
本例需在 本地离线 Docker 容器(debian:bookworm-slim,GNU bash 5.2.15,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地离线 Docker 容器(debian:bookworm-slim,GNU bash 5.2.15,--network none)。
入口文件:projects/linux-file-attr-permission-lab/explore-file-attr-permission.sh
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-file-attr-permission.sh,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(debian:bookworm-slim,GNU bash 5.2.15,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联32章) · 含当前章节或实例 · mbskycom202612a1487.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1491.zip