蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 GNU bash 5.2 点号(.)无执行位包含与 source 同进程环境共享实测

BASH本地离线 Docker 容器(debian:bookworm-slim,GNU bash 5.2.15,--network none)。explore-shell-include-file.sh
explore-shell-include-file.sh83 行
#!/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: dot_include_without_exec_bit - . ./test1.sh works when test1.sh is 0644 (non-executable)
cat <<'EOF' > "$WORKDIR/test1.sh"
#!/bin/bash
url="https://docs.example.org"
greet_lib() {
  echo "lib:$url"
}
EOF
chmod 0644 "$WORKDIR/test1.sh"

cat <<'EOF' > "$WORKDIR/test2.sh"
#!/bin/bash
. ./test1.sh
echo "url=$url;fn=$(greet_lib)"
EOF
chmod 0755 "$WORKDIR/test2.sh"

is_t1_exec=$([ -x "$WORKDIR/test1.sh" ] && echo 1 || echo 0)
out_dot=$(cd "$WORKDIR" && ./test2.sh)
add_case \
  "dot_include_without_exec_bit" \
  "验证被包含脚本 test1.sh 无需可执行权限(0644),主脚本通过 . ./test1.sh 可读取其变量与函数" \
  "t1_exec=0;out=url=https://docs.example.org;fn=lib:https://docs.example.org" \
  "t1_exec=${is_t1_exec};out=${out_dot}"

# Case 2: source_include_same_shell_env - source executes in current shell process (same PID, mutates caller vars)
cat <<'EOF' > "$WORKDIR/env_mod.sh"
INCLUDED_PID="$$"
SHARED_STATE="updated_in_included"
EOF
chmod 0644 "$WORKDIR/env_mod.sh"

SHARED_STATE="initial"
CALLER_PID="$$"
source "$WORKDIR/env_mod.sh"
same_pid=$([ "$CALLER_PID" = "$INCLUDED_PID" ] && echo 1 || echo 0)
add_case \
  "source_include_same_shell_env" \
  "验证 source 在当前 Shell 进程上下文执行(\$\$ 相同),且被包含文件中的变量变更直接作用于调用方" \
  "same_pid=1;state=updated_in_included" \
  "same_pid=${same_pid};state=${SHARED_STATE}"

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-include-file-lab/explore-shell-include-file.sh

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-shell-include-file.sh,或下载右侧课程工程包。
  2. 确认本地已准备 本地离线 Docker 容器(debian:bookworm-slim,GNU bash 5.2.15,--network none)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Linux

课程关联工程包(登记关联32章) · 含当前章节或实例 · mbskycom202612a1487.zip

下载课程关联工程包:Linux

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1506.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习