I edit it in the Windows editor and then upload it to Linux for execution.
#!/bin/bash
Monitor()
{
pgrep -x mysqld &> /dev/null
if [ $?-ne 0 ];
then
echo "At time: `date` :MySQL is stop .">> /opt/service/logs/mysqlMonitor.log
/sbin/service mysqld start
else
echo "MySQL server is running ."
fi
echo "-----------------------------------"
}
Monitor
This error has been reported since Linux was executed.
mysqlmonitor.sh: line 9: syntax error near unexpected token `fi’
mysqlmonitor.sh: line 9: `fi’
At the beginning, I thought the Linux command was wrong. I looked up a lot of information. It was a simple statement, and I was sure it was right. But why report such an error?
I took the cat-v mysqlmonitor.sh command to see my code
Monitor()^M
{^M
pgrep -x mysqld &> /dev/null^M
if [ $?-ne 0 ];then^M
echo "At time: `date` :MySQLM-BM- is stop .">> /opt/service/logs/mysqlMonitor.log^M
/sbin/service mysqld start^M
else^M
echo "MySQL server is running ."^M
fi^M
echo "-----------------------------------"^M
}^M
Monitor^M
Found in Linux after the automatic view in the end of the line with ^M identity, through looking up the information that in Windows and Linux is not the same newline. The details can also be confirmed by the command again.
Open the file with Vim and type in command line mode
What we found was DOS
Now set our code mode with the command: set FF = Unix
And then save the code
Look again and discover that the code is programmed for Unix
Cat -v **.sh to see that the code is normal.
There are no errors in executing the code again.
If you think it’s working, check out this article below. You can also consult me through QQ: 772757263
Read More:
- Solve the problem of syntax error: unexpected end of file or syntax error near unexpected token ` fi ‘error
- Shell script syntax error near unexpected token ‘$’Do
- Shell script execution error: “syntax error near unexpected token”
- Syntax error near unexpected token `newline’script cannot be executed
- syntax error near unexpected token `then’ problem solution
- Docker machine install syntax error near unexpected token ‘newline’! DOCTYPE HTML problem resolution
- Syntax error near unexpected token ‘$’Do is reported when running bash file
- Solve the Java “syntax error on token enum” problem
- Solution to shell script error “expr: syntax error”
- Unexpected syntax error: unexpected token<
- syntax error near unexpected token `else’
- Uncaught syntax error: unexpected token ‘< 0‘
- Shell script: syntax error: bad for loop variable error resolution
- Syntax error: unexpected token in uni app project compilation
- Syntax error: unexpected end of file problem [two solutions]
- [shell] sh executes the script and reports an error syntax error: “(” unexpected “)
- Error: unexpected syntax error: unexpected token<
- About writing [if] and [else] statements in JSP, Syntax error on token “else”, delete this token
- Solve the problem of syntax error: invalid syntax in PIP install XXX
- Error: syntax error – unexpected token P in JSON at position 0