【Linux】Shell脚本“syntax error: unexpected end of file”原因及处理
2018-03-21 13:25
666 查看
脚本调试老不好,多半是要废了......
背景:
项目需要部署新的后台服务,于是开始编写shell部署脚本,然后把写好的脚本放到服务器上,发现不能执行一直报错。但是当我vim查看脚本,并没有第51行,反复执行总是提示错误,内心是拒绝的。
原因:
最后发现我的脚本是在window环境下编写的,然后传到linux服务器上的,这时候问题来了,doc下的文本内容格式和unix下的格式有所不同,比如dos文件传输到unix系统时,会在每行的结尾多一个^M结束符。(我的就是这个原因)解决:
vim serverDeploy.sh :set fileformat=unix :wq或者
dos2unix命令将dos文件转换为unix格式(dos2unix需要安装:yum -y install dos2unix)
dos2unix serverDeploy.sh重新执行脚本就行可以了sh -x serverDeploy.sh
建议:
1.最好在linux下编写shell脚本,如果喜欢window下的ide,记得上传脚本到服务器时,记得修改脚本的格式,和执行权限;2.可以使用 sh -n serverDeploy.sh 命令来检查脚本的语法;
3.注意shell的中的变量引用,如:[ -z $VAR ]
字符串变量在使用时请养成加双引号的习惯 "$VAR",这样无论是字符串为空或是字符串中含有空格,都不会有任何问题。改为:
if [ -z "$PASODSPATH" ]; then
或者:
if [ -z "$PASODSPATH" ]
then
相关文章推荐
- linux-shell syntax error: unexpected end of file
- SHELL syntax error:unexpected end of file 提示错误; windows下修改linux文件导致linux文件无法使用
- shell脚本“syntax error:unexpected end of file”解决方案
- shell脚本“syntax error:unexpected end of file”解决方案
- 在执行shell脚本时,出现“syntax error : unexpected end of file"错误的解决方法
- Shell脚本:“syntax error:unexpected end of file”
- linux脚本相关: Syntax error: end of file unexpected (expecting "then") 提示错误
- shell脚本调试报错:"syntax error: unexpected end of file"
- 【LINUX】SHELL syntax error:unexpected end of file
- SHELL脚本“SYNTAX ERROR:UNEXPECTED END OF FILE”解决方案
- Shell中EOF出现syntax error: unexpected end of file
- 解决执行脚本报syntax error: unexpected end of file或syntax error near unexpected token `fi'错误的问题
- shell:syntax error:unexpected end of file/Starting proxy www-balancer: cannot bind socket--转载
- 关于shell文件中出现syntax error: unexpected end of file
- SHELL syntax error:unexpected end of file 提示错误
- shell:syntax error:unexpected end of file/Starting proxy www-balancer: cannot bind socket--转载
- shell中出现syntax error: unexpected end of file
- Shelll 错误 syntax error: unexpected end of file
- syntax error: unexpected end of file windows linux换行 文件格式
- SHELL syntax error:unexpected end of file 提示错误