解决执行脚本报syntax error: unexpected end of file或syntax error near unexpected token `fi'错误的问题
来源:互联网 发布:搬瓦工安装nginx 编辑:程序博客网 时间:2024/05/08 02:36
我们在编辑Linux服务器上的文件时,如果直接在Linux环境下编辑的话,比较麻烦,大段粘贴格式比较乱,还得自己一行一行去调整,因此会选择用编辑器来编辑我们Linux环境下的文件,我用的是NodePad++,大家可以参考http://blog.csdn.net/u012453843/article/details/52987666这篇博客来学习如何使用NodePad++来编辑Linux系统的文件。
今天我要说的重点是,我们在用NodePad++编辑完脚本文件并上传到服务器后,直接运行脚本报错的问题,比如我要在NodePad++编辑器中编辑下面这段脚本
#!/bin/shA=`ps -C nginx --no-header |wc -l`if [ $A -eq 0 ];then/usr/local/nginx/sbin/nginxsleep 2if [ `ps -C nginx --no-header |wc -l` -eq 0 ];thenkillall keepalivedfifi先来说说第一个错误:syntax error: unexpected end of file,大家可以把上面那段脚本内容放到脚本文件nginx_check.sh当中(当然大家可以随便起个名字,后缀是.sh就可以了),需要注意的是最后一个"fi",默认是黑色字体,我们敲回车增加一行,这时最后那个"fi"颜色就变成蓝色了,如下图所示。
编辑完该脚本我们保存,自动就会上传到Linux服务器,下面我们便在Linux服务器执行该脚本(需要注意的是,该脚本文件一定要有执行权限,如果没有执行权限就使用chmod +x nginx_check.sh来给脚本增加执行权限),我们会看到如下所示的错误,提示结尾不合法。
[root@bhz004 keepalived]# sh ./nginx_check.sh ./nginx_check.sh: line 10: syntax error: unexpected end of file[root@bhz004 keepalived]#我们再来看另外一个错误:syntax error near unexpected token `fi',大家可以直接把脚本内容粘贴到脚本文件中,最后一个"fi"此时是黑色的,我们不用管它。
这时我们执行该脚本,会看到如下所示错误,提示第9行"fi"有问题。
[root@bhz004 keepalived]# sh ./nginx_check.sh ./nginx_check.sh: line 9: syntax error near unexpected token `fi'./nginx_check.sh: line 9: `fi'[root@bhz004 keepalived]#上面两个问题都是由于.sh文件的格式为dos格式。而linux只能执行格式为unix格式的脚本。因为在dos/window下按一次回车键实际上输入的是“回车(CR)”和“换行(LF)”,而Linux/unix下按一次回车键只输入“换行(LF)”,所以修改的sh文件在每行都会多了一个CR,所以Linux下运行时就会报错找不到命令。
我们可以查看该脚本文件的格式,方法是使用命令:vim nginx_check.sh进入编辑文件界面,如下图所示。
直接输入":",然后在":"之后输入"set ff"如下图所示
回车即可看到脚本格式,如下图所示,可以看到当前脚本格式是dos。
我们需要把格式改为unix,方法是输入":set ff=unix",也可以输入":set fileformat=unix"如下图所示。
输入完之后,回车即可完成切换格式。然后我们再输入":set ff"来查看格式,如下图所示,可以看到当前脚本格式变成了我们想要的"unix"了。
这时我们再执行nginx_check.sh就没问题了,如下所示。
[root@bhz004 keepalived]# sh ./nginx_check.sh [root@bhz004 keepalived]# ps -ef | grep nginxroot 3679 1 0 19:42 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginxnobody 3681 3679 0 19:42 ? 00:00:00 nginx: worker process root 3711 1898 0 20:23 pts/0 00:00:00 grep nginx[root@bhz004 keepalived]#
- 解决执行脚本报syntax error: unexpected end of file或syntax error near unexpected token `fi'错误的问题
- 脚本报syntax error near unexpected token `(',
- syntax error near unexpected token 和 Linux Shell syntax error: unexpected end of file
- syntax error : unexpected end of file 问题的解决
- shell中syntax error near unexpected token `fi'错误
- syntax error near unexpected token `then'问题的解决
- syntax error near unexpected token `问题的解决
- syntax error near unexpected token `then'问题的解决
- syntax error near unexpected token `then'问题的解决
- syntax error near unexpected token `then'问题的解决
- syntax error near unexpected token `then'问题的解决
- syntax error near unexpected token `then'问题的解决
- linux执行SHELL脚本报错“syntax error near unexpected token `in”解决
- linux执行SHELL脚本报错“syntax error near unexpected token `in”解决
- linux执行SHELL脚本报错“syntax error near unexpected token `in”解决
- 在执行shell脚本时,出现“syntax error : unexpected end of file"错误的解决方法
- 解决shell错误:syntax error: unexpected end of file
- 错误:syntax error near unexpected token `then'
- win10常用技巧
- Python爬虫学习一——requests库
- Cannot invoke Tomcat manager 和Connection refused: connect
- 云服务器上Tomcat如何隐藏8080端口号
- require.js的使用
- 解决执行脚本报syntax error: unexpected end of file或syntax error near unexpected token `fi'错误的问题
- 如何选择安全座椅
- matlab图像处理之image和imagesc函数
- 搭建单机版fastDFS环境
- java实现多线程
- display:inline的用法
- 数据集资源
- 矩阵特征值和特征向量求解——特征值分解
- Android 7.1.1时间更新NITZ和NTP详解 .