Files
simulation_core/setup_dotnet_env.sh
2026-03-13 13:54:49 +08:00

66 lines
1.7 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
set -euo pipefail
RC_FILE="${RC_FILE:-$HOME/.bashrc}"
DOTNET_DIR="${DOTNET_DIR:-$HOME/.dotnet}"
begin_marker="# >>> dotnet user-local env >>>"
end_marker="# <<< dotnet user-local env <<<"
block_content=$(cat <<EOF
${begin_marker}
export DOTNET_ROOT="${DOTNET_DIR}"
export PATH="\$DOTNET_ROOT:\$DOTNET_ROOT/tools:\$PATH"
${end_marker}
EOF
)
touch "${RC_FILE}"
# 幂等更新:如果已有块就删掉再写入
if grep -Fq "${begin_marker}" "${RC_FILE}"; then
tmp="$(mktemp)"
awk -v b="${begin_marker}" -v e="${end_marker}" '
BEGIN{inblk=0}
index($0,b){inblk=1; next}
index($0,e){inblk=0; next}
inblk==0{print}
' "${RC_FILE}" > "${tmp}"
mv "${tmp}" "${RC_FILE}"
fi
{
echo
echo "${block_content}"
} >> "${RC_FILE}"
# ✅ 关键修复:不要依赖 source ~/.bashrc非交互可能直接 return
# 直接在当前脚本进程里让环境变量生效
export DOTNET_ROOT="${DOTNET_DIR}"
export PATH="${DOTNET_ROOT}:${DOTNET_ROOT}/tools:${PATH}"
echo "== DOTNET_ROOT =="
echo "${DOTNET_ROOT}"
echo
echo "== which dotnet =="
command -v dotnet || true
echo
# 检测 dotnet 是否实际安装在 ~/.dotnet
if [[ ! -x "${DOTNET_ROOT}/dotnet" ]] && ! command -v dotnet >/dev/null 2>&1; then
echo "ERROR: dotnet 仍然不可用。"
echo "原因通常是:${DOTNET_ROOT}/dotnet 不存在(还没安装 .NET SDK/Runtime"
echo
echo "你可以检查:"
echo " ls -lh ${DOTNET_ROOT}"
echo
echo "如果你本来是装在别的目录(例如 /usr/share/dotnet就把 DOTNET_DIR 改成那个路径:"
echo " DOTNET_DIR=/usr/share/dotnet ./setup_dotnet_env.sh"
exit 2
fi
echo "== dotnet --info =="
dotnet --info
echo
echo "Done. Persistent config written to: ${RC_FILE}"