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

Shell脚本中read配合EOF使用报syntax error near unexpected token `;'如何解决

问题原因
  • 你使用了<<EOF的Here Document语法给kubectl传递配置内容,但缺少Here Document必须的结束标记EOF。Bash会把<<EOF之后的所有内容都判定为要传给kubectl的输入流,直到遇到单独占一行、顶格书写的EOF才会结束输入识别。你原来的脚本直接在YAML配置末尾写了; done,这部分内容会被当成配置内容传给kubectl,Bash没有读到合法的循环结束语法,就会在碰到非法的;字符时报错。
  • 注意EOF结束标记必须单独占一行,且行首不能有任何缩进、空格、制表符,否则不会被Bash识别为Here Document的结束标记。
修复后的脚本
while read -r name port ignored; do
kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: $name
  namespace: nussa
spec:
  hosts:
    - $name.nussa.svc.cluster.local
  http:
    - route:
        - destination:
            host: $name
            port:
              number: $port
            subset: v1
EOF
done < svc1.data

内容的提问来源于stack exchange,提问作者White Mask Guy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 07:24:02