[Linux]Shell脚本“syntax error: unexpected end of file”报错及处理
问题描述
项目在Window下开发,写了几个bash脚本移到Linux环境下使用bash运行,提示报错:
syntax error:unexpected end of file
或者添加可执行权限直接运行脚本时提示:
/bin/bash^M: bad interpreter: No such file or directory
可以看出shell脚本在linux下每行末尾多了一个‘^M’结束符,这是由于编码格式不同导致的。
解决方式
vim 打开脚本,输入
:set fileformat=unix
然后wq保存,问题解决。