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

Concourse任务Shell脚本读取同目录文件时提示文件未找到错误

Concourse脚本找不到同目录配置文件的解决思路

问题场景

Concourse任务配置:

name: test
plan:
  - get: my-repo
  - task: my-task
    config:
      inputs:
      - name: my-repo  
      run:
        path: sh
        args: [my-repo/examples/run-this.sh]

脚本run-this.sh中尝试读取同目录的name.cfg:

CONFIG_FILE=./$name.cfg

执行任务时抛出错误:

my-repo/examples/run-this.sh: line xx: can't open name.cfg: no such file

问题原因

Concourse任务的默认工作目录是任务根目录(即my-repo所在的目录),脚本执行时,./指向的是任务根目录,而非脚本所在的my-repo/examples/目录,因此找不到同目录的配置文件。

解决方案

  • 调整脚本路径基准:在脚本开头获取自身所在目录,基于该目录构建配置文件路径

    # 获取脚本所在的绝对目录
    SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
    CONFIG_FILE="${SCRIPT_DIR}/name.cfg"
    
  • 修改任务执行时的工作目录:通过cd切换到脚本所在目录后再执行脚本

    run:
      path: sh
      args:
        - -c
        - "cd my-repo/examples && ./run-this.sh"
    
  • 直接使用任务根目录的完整相对路径:如果脚本位置固定,直接写全路径指向配置文件

    CONFIG_FILE="./my-repo/examples/name.cfg"
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 17:01:36