SHELL脚本“SYNTAX ERROR:UNEXPECTED END OF FILE”解决方案

xiaoxiao2021-02-28  135

今天,自己在写一个一键安装http服务的脚本时,使用if语句编写,用bash -x 命令你给调试,都报错“SYNTAX ERROR:UNEXPECTED END OF FILE”,怎么改动都会报错,通过google,发现自己的错误了。

1、在最初脚本中是在windows下,使用sublime编辑器写。 将shell脚本传至linux中:使用命令bash -x SCRIPT 检查语法总是出一个错误 syntax error:unexpected end of file。

原因如下:

dos文件传输到unix系统时,会在每行的结尾多一个^M,在vim的时候,当你用如下命令查看:cat -A SCRIPT 示例: [root@localhost~]#cat -A installhttp.sh

#!/bin/bash^M$ #The script is autoinstallhttp.sh^M$ #Author Joah^M$ #Version 1.0.0^M$ version=`grep -o " [0-9]\>" /etc/centos-release`^M$ if [ $version -eq 7 ];do^M$ echo "We will install newversion.Please wait!"^M$

2、解决方法 使用命令dos2unix命令将SCRIPT脚本格式

[root@localhost~]#dos2unix installhttp.sh dos2unix: converting file installhttp.sh to UNIX format ...

或者: [root@localhost/app]#vim installhttp.sh :set fileformat=unix :wq [root@localhost/app]#cat -A installhttp.sh

$ $ #/bin/bash$ #$ #The script is autoinstallhttp.sh$ #Author Joah$ #Version 1.0.0$ version=`grep -o " [0-9]\>" /etc/centos-release$
转载请注明原文地址: https://www.6miu.com/read-25462.html

最新回复(0)