执行linux脚本(sh文件)报错:syntax error: unexpected end of file

用shell编写的一个脚本,执行的时候报错:syntax error: unexpected end of file!

发生这种报错有两种原因:

第一种:

如果你是用== windows 系统编写的脚本==,那么就有可能是 fileformat 类型不同!可能是 fileformat=dos ,那么你需要把格式修改为unix,使用方法:
vim xx.sh
esc > : > set ff=unix > enter > 文件结尾
保存退出

第二种:

如果你是用== Linux 系统编写的 shell 脚本==,在 vim 的命令模式下,执行 :set ff 就能看到 fileformat 类型是 unix ,那么恭喜你,你的 shell 脚本里存在语法错误,你可以使用二分法逐段检查错误,具体就是注释一部分,留一部分,然后执行 sh -n 脚本名字 来检查错误,知道排查处错误为止!

猜你喜欢

转载自blog.csdn.net/Shiny_boy_/article/details/129320385