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

Docker运行Shell脚本报错:unexpected "(" (expecting "}")排查

问题

尝试使用Docker运行Shell脚本,Dockerfile内容如下:

FROM alpine
COPY . .
RUN chmod +x ./scripts/sanity-checks.sh
ENTRYPOINT ["./scripts/sanity-checks.sh"]

运行时出现错误:

./scripts/sanity-checks.sh: scripts/util.sh: line 225: syntax error: unexpected "(" (expecting "}")

util.sh中对应代码片段为:

...  
# delete the lines between and including 2 comment lines, or the comment lines themselves only
    delete_lines_for_file() {
      file_name=$1
      delete_string_start=$2
      delete_string_end=$3
      delete_comments_only=$4
      # find the start and end lines where we want to delete blocks of code
      line_num_start=$(grep -n "$delete_string_start" $file_name | awk -F  ":" '{print $1}')
      line_num_end=$(grep -n "$delete_string_end" $file_name | awk -F  ":" '{print $1}')
      line_start_arr=($line_num_start) ### LINE 225 ###
...

该脚本在本地直接运行正常,请问报错原因是什么?

原因分析与解决办法
  • 核心原因:Alpine Linux默认使用ash shell,而你本地运行脚本用的是bash。ash不支持line_start_arr=($line_num_start)这种bash风格的数组初始化语法,因此触发语法错误。
  • 解决办法:
    1. 切换到带bash的基础镜像:
      • 方案一:使用debian镜像(自带bash)
        FROM debian:stable-slim
        COPY . .
        RUN chmod +x ./scripts/sanity-checks.sh
        ENTRYPOINT ["./scripts/sanity-checks.sh"]
        
      • 方案二:在Alpine中安装bash
        FROM alpine
        RUN apk add --no-cache bash
        COPY . .
        RUN chmod +x ./scripts/sanity-checks.sh
        ENTRYPOINT ["bash", "./scripts/sanity-checks.sh"]
        
    2. 修改脚本适配ash:如果坚持使用ash,需要调整数组相关语法,比如用set命令实现变量拆分:
      # 替换原数组初始化行
      set -- $line_num_start
      line_start_arr=("$@")
      

内容的提问来源于stack exchange,提问作者snack-overthrow

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 09:30:40