Solve the problem of syntax error: unexpected end of file or syntax error near unexpected token ` fi ‘error

When editing a file on Linux server, it would be troublesome to edit it directly in Linux environment. The format of large paste is messy, and we have to adjust it line by line, so we will choose to edit the file in our Linux environment with editor. I use NodePad++, You can refer to this blog to learn how to use the http://blog.csdn.net/u012453843/article/details/52987666 NodePad++ to edit the file that Linux system.
The main thing I’m going to talk about today is the problem of running the script error directly after we’ve edited the script file with NodePad++ and uploaded it to the server. For example, I’m going to edit the following script in the NodePad++ editor

#!/bin/sh
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
	/usr/local/nginx/sbin/nginx
	sleep 2
	if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
		killall keepalived
	fi
fi

First said the first error: syntax error: unexpected end of file, you can put that script content above script file nginx_check. Of sh (suffix, of course, you can literally a name. Sh is ok), it is important to note the last “fi”, the default is black font, we press enter increase a line, then the last “fi” color to blue, as shown in the figure below.


Editing the script we save, automatically uploaded to a Linux server, below we will execute the script in the Linux server (it is important to note that the script file must have execute permissions, you use without execute permissions chmod + x nginx_check. Sh to add execute permissions to the script), we will see the following error, tip end is illegal.

[root@bhz004 keepalived]# sh ./nginx_check.sh 
./nginx_check.sh: line 10: syntax error: unexpected end of file
[root@bhz004 keepalived]# 

Let’s look at another error: syntax error near token ‘fi’, you can paste the contents of the script directly into the script file, the last’ FI ‘is black at this time, we will not care about it.


when we execute this script, we will see the following error, indicating that line 9 “fi” has a problem.

[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]# 

Both of the above problems are due to the format of the.sh file in DOS format. Linux, on the other hand, can only execute scripts in Unix format. Because pressing the enter key once in DOS/Window actually enters “Enter (CR)” and “Line feed (LF)”, and Linux/ Unix only enters “Line feed (LF)”, the modified SH file will have one more CR per line, so it will report an error and not find the command when running under Linux.

You can view the format of the script file by entering the edit file interface using the command vim nginx_check-sh, as shown below.

Type “:” directly, then “set FF “after “:” as shown below

Press enter to see the script format, as shown in the figure below, and you can see that the current script format is DOS.

We need to change the format to Unix by either typing “set FF = Unix” or “Set FileFormat = Unix” as shown below.

After input, press enter to complete the format switch. Then we type “:set FF “to see the format, as shown in the figure below, and you can see that the current script format is now the” Unix “we want.

Then we can execute nginx_check-sh as shown below.

[root@bhz004 keepalived]# sh ./nginx_check.sh 
[root@bhz004 keepalived]# ps -ef | grep nginx
root       3679      1  0 19:42 ?       00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody     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]# 

Read More: