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]#
div>
Read More:
- Solve the problem of shell script “syntax error near unexpected token `fi’”.
- Solve syntax error: unexpected end of file or /bin/bash^m: bad interpeneter: no match file or directory
- Syntax error: unexpected end of file problem [two solutions]
- Shell script syntax error near unexpected token ‘$’Do
- syntax error near unexpected token `then’ problem solution
- Syntax error near unexpected token ‘$’Do is reported when running bash file
- Syntax error near unexpected token `newline’script cannot be executed
- [!] Invalid `Podfile` file: syntax error, unexpected end-of-input, expecting keyword_end.
- Unexpected syntax error: unexpected token<
- Shell script execution error: “syntax error near unexpected token”
- Docker machine install syntax error near unexpected token ‘newline’! DOCTYPE HTML problem resolution
- Syntax error: unexpected token in uni app project compilation
- Parse error: syntax error, unexpected end of file in XXXXXXXX
- Uncaught syntax error: unexpected token ‘< 0‘
- Error: unexpected syntax error: unexpected token<
- syntax error near unexpected token `else’
- Baidu editor echo JS error uncaught syntax error: invalid or unexpected token
- Error: syntax error – unexpected token P in JSON at position 0
- [development experience] solution to unexpected syntax error: unexpected identifier in JSON parsing
- “Unexpected syntax error: unexpected identifier”.