The shell script is edited on the local computer in DOS format (you can edit the shell script with VI, enter the command line mode, and enter: set FF Press enter to see fileformat = DOS in the lower left corner. Press enter once in DOS / window, but in Linux / Unix, press enter once only to enter line feed (LF). Therefore, if you run the locally modified sh file on CentOS, there will be one more CR for each line. Therefore, syntax error: unexpected end of of file。
The solution is very simple, open the shell script with VI editor, enter the command line mode (Ctrl + C), enter: set FF = UNIX, then WQ save and exit. At this point, if you check through set FF again, you will find that fileformat = UNIX.
The following is the supplement of other netizens
When we edit files on Linux server, if we edit directly in Linux environment, it is more troublesome, and the paste format of large sections is messy. We have to adjust them one by one line. Therefore, we will choose to edit the files in our linux environment by using editor. I use nodepad++. You can refer to this article https://www.jb51.net/softjc/712049.html To learn how to use n Odepad++ to edit the files of Linux systems.
What I want to talk about today is that after editing the script file with nodepad + + and uploading it to the server, we can directly run the script to report errors. For example, I want 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
Let’s talk about the first error: syntax error: unexpected end of file. You can put the above script in the script file nginx_ check.sh Among them (of course, you can choose any name. The suffix is. SH). We need to pay attention to the last “fi”, which is in black font by default. We press enter to add a line. At this time, the color of the last “fi” turns blue, as shown in the figure below.
After editing the script, we save it and automatically upload it to the Linux server. Next, we execute the script on the Linux server (note that the script file must have the execution permission. If not, use Chmod + X nginx_ check.sh To add execution permission to the script), we will see the following error, indicating that the end is illegal.
Let’s look at another error: syntax error near unexpected token ` fi ‘. You can paste the script content directly into the script file. The last “fi” is black at this time. We don’t need to worry about it.
At this point, we execute the script and see the error as follows, prompting line 9 “fi” to have a problem.
[[email protected] keepalived]# sh ./nginx_check.sh ./nginx_check.sh: line 9: syntax error near unexpected token `fi' ./nginx_check.sh: line 9: `fi' [[email protected] keepalived]#
Both of the above problems are due to the DOS format of the. Sh file. Linux can only execute scripts in UNIX format. Because pressing the Enter key once in DOS / window actually inputs “CR” and “LF”, while in Linux / Unix, pressing the Enter key only inputs “line feed (LF)”, so the modified sh file will have one more Cr in each line, so when running under Linux, it will report an error and cannot find the command.
We can see the format of the script file by using the command: VIM nginx_ check.sh Enter the edit file interface, as shown in the figure below.
Enter “:” directly, and then enter “set FF” after “:”, as shown in the figure below
Enter to see the script format, as shown in the following figure, you can see that the current script format is DOS.
We need to change the format to UNIX by entering “: set FF = UNIX” or “: set fileformat=unix” as shown in the following figure.
After input, press enter to complete the format switching. Then we enter “: set FF” to view the format. As shown in the figure below, we can see that the current script format has become the “UNIX” we want.
Then we execute nginx_ check.sh No problem, as shown below.
[[email protected] keepalived]# sh ./nginx_check.sh [[email protected] 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 [[email protected] keepalived]#
This article introduced this, basically is to pay attention to the coding problem and line feed problem, can not use the text editor.