[Solved] Shell error: syntax error: unexpected end of file

Sometimes an error will be reported when executing the script:

[[email protected] shell]# sh -x test. sh  
+ $ ' \r '
: command not found
test. sh : line 37 : syntax error: unexpected end of file

reasons may be:

The text editor is the file written by Notepad or other editors under windows, and it runs in the cygwin simulation LINUX software environment.

Solutions:

The file format difference between DOS and Linux is caused.

The text file under DOS uses \r\n as the line break mark, expressed in hexadecimal is 0D 0A. The text file under Unix uses \n as a line break mark, which is 0A in hexadecimal.

[In windows, the two symbols used for line feed, carriage return \r, line feed symbol \n, under linux, only one symbol \n is enough .]

A text file in DOS format under Linux will display ^M at the end of the line when opened with a lower version of vi. Of course, you may not be able to see it, but in vi, the format of the file will be displayed below, “M.txt “[dos] 8L, 72C means a dos file format.

solution:

Use the following command to set the file format to unix format to solve the above error

vi test. sh 
:set fileformat = unix
:wq

Why can’t the script edited under windows be directly copied to the unix system and run directly?

Linux executes SHELL script error “syntax error near unexpected token `in”

long long ago….. Old teletypewriters used two characters to start a new line. One character moves the carriage back to the first position (called carriage return, <CR>, ASCII code 0D), and another character moves the paper up one line (called line feed, <LF>, ASCII code 0A). When computers came out, memory used to be very expensive. Some people think it is unnecessary to use two characters to indicate the end of a line. Unix developers decided that they could use a character to indicate the end of a line, Linux followed Unix, and it was also <LF>. Apple developers specified the use of <CR>. The guys who developed MS-DOS and Windows decided to use the old <CR><LF>.

Because MS-DOS and Windows use carriage return + line feed to represent line feed, so under Linux, use Vim to view the code written in VC under Windows. The “^M” symbol at the end of the line indicates a symbol.


Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *