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

Shell脚本报syntax error near unexpected token 'done'是什么原因?

报错原因

  • Shell 中 while 循环的标准语法要求条件语句和循环体之间必须用 do 关键字标记循环体的起始位置,固定结构为:
    while 条件命令
    do
      循环体命令
    done
    
  • 你使用的 while : 本身是正确的死循环写法,: 是 Shell 内置空命令,执行后永远返回0,等价于 true。你的代码遗漏了 do 关键字,导致 Shell 解析到 done 时找不到与之配对的循环起始标记,因此抛出语法错误。

修复方案

两种可行写法:

  1. 单行写法,补充do关键字即可:
    while :; do echo 'wtf'; done
    
  2. 多行可读性更高的写法:
    while :
    do
      echo 'wtf'
    done
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 05:45:03