无第16行却报‘line 16: syntax error: unexpected end of line’错误求助
Shell脚本语法错误排查
问题场景
执行下面的Shell脚本时,明明脚本没有第16行,却弹出错误line 16: syntax error: unexpected end of line。试过用dos2unix转换格式、额外添加fi,还是报同样的错误。
原脚本
a=10 b=20 c=30 if [[ $a -gt $b && $a -gt $c ]] then echo "A is the greater number" if [[ $b -gt $a && $b -gt $c ]] then echo "B is the greater number" else echo "C is the greate number" fi
格式化后的代码(Ed Morton补充)
a=10 b=20 c=30 if [[ $a -gt $b && $a -gt $c ]] then echo "A is the greater number" if [[ $b -gt $a && $b -gt $c ]] then echo "B is the greater number" else echo "C is the greate number" fi
问题原因
从格式化后的代码可以直接看出:最外层的if语句没有对应的闭合fi。脚本仅给嵌套的内层if添加了结束标记,外层if全程未闭合,Shell解析到脚本结尾仍找不到外层if的结束符,因此触发语法错误。
修复方案
在脚本末尾添加一个fi,闭合最外层的if即可:
a=10 b=20 c=30 if [[ $a -gt $b && $a -gt $c ]] then echo "A is the greater number" if [[ $b -gt $a && $b -gt $c ]] then echo "B is the greater number" else echo "C is the great number" # 顺便修正拼写错误:greate→great fi fi # 新增此行,闭合外层if
内容的提问来源于stack exchange,提问作者Gaddam Abhishek
相关产品推荐
相关产品推荐

