You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker容器内source .sh脚本仍报错「需以source执行」的求助

问题

在Docker容器中执行source ./install.sh时触发错误,提示脚本必须通过source方式运行,但实际已使用source命令。

复现步骤

执行以下命令序列:

$ docker run -i -t conda/miniconda3 /bin/bash

# apt-get update
# apt-get install git
# git clone https://github.com/guicho271828/latplan.git
# cd latplan
# source ./install.sh

报错内容

This script must be sourced, not executed. Run it like: source /bin/bash

install.sh开头代码

#!/bin/bash

env=latplan

# execute it in a subshell so that set -e stops on error, but does not exit the parent shell.
(
    set -e

    (conda activate >/dev/null 2>/dev/null)  || {
        echo "This script must be sourced, not executed. Run it like: source $0"
        exit 1
    }

    conda env create -n $env -f environment.yml || {
        echo "installation failed; cleaning up"
        conda env remove -n $env
        exit 1
    }

    conda activate $env

    git submodule update --init --recursive
解决方案

问题根源在于脚本的两个设计冲突:

  1. 脚本用**子shell(外层的())**包裹核心逻辑,而conda的activate命令只能在当前shell环境运行,子shell无法执行conda activate,导致检测逻辑失败,触发错误提示。
  2. 即便用source运行脚本,子shell仍是独立进程,无法继承父shell的conda环境上下文。

修复方案一:移除子shell包裹

直接删除脚本最外层的(和对应的),让所有命令在当前shell环境执行,修改后的脚本开头如下:

#!/bin/bash

env=latplan

set -e

(conda activate >/dev/null 2>/dev/null)  || {
    echo "This script must be sourced, not executed. Run it like: source $0"
    exit 1
}

conda env create -n $env -f environment.yml || {
    echo "installation failed; cleaning up"
    conda env remove -n $env
    exit 1
}

conda activate $env

git submodule update --init --recursive

保存修改后重新执行source ./install.sh即可正常运行。

修复方案二:提前初始化conda shell钩子

在执行source ./install.sh前,先运行以下命令初始化conda的shell环境:

source /opt/conda/etc/profile.d/conda.sh

这会让conda activate命令在当前shell及子shell中都能正常执行,绕过原脚本的检测错误。

内容的提问来源于stack exchange,提问作者ailauli69

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 03:54:15