Tag Archives: syntax error: unexpected end of file

shell :syntax error: unexpected end of file

Sometimes an error is reported when executing a script:

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

The reason may be:

Text editing is written through Notepad or other editors under windows. This file runs in cygwin simulated linux software environment

Solution:

The file format difference between dos and Linux

Text files under DOS take \ R \ n as the line break flag, expressed in hexadecimal, which is 0d 0A. Text files under UNIX take \ n as the line break flag, which is expressed in hexadecimal, which is 0A

[in windows, two symbols for line feed, enter \ R, line feed symbol \ n, only one symbol is required under Linux .]

Text files in DOS format will display ^ m at the end of the line when they are opened with a lower version of VI under Linux. Of course, they may not be visible, but when they are opened with VI, the format of this file will be displayed below, “m.txt” [DOS] 8L, 72C indicates that it is 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 scripts edited under windows be directly copied to UNIX system and run directly

Linux executes shell script and reports “syntax error near unexpected token ` in”

Old teletypewriters use two characters to start a new line. One character moves the carriage back to the first position (called enter, & lt; CR>, ASCII code is 0d), and another character moves the paper up one line (called line feed, & lt; LF>, ASCII code is 0A). When computers came out, memory used to be very expensive. Some people think it is unnecessary to use two characters to represent the end of a line. UNIX developers decide that they can use one character to represent the end of the line. Linux follows UNIX, which is also & lt; LF>。 Apple developers have specified & lt; CR>。 The guys who developed MS-DOS and windows decided to follow the old & lt; CR>& lt; LF>。

Because MS-DOS and windows use carriage return + line feed to represent line feed, use VIM to view the code written in VC under windows under Linux. The “^ m” symbol at the end of the line represents the symbol

shell :syntax error: unexpected end of file

 

Sometimes an error is reported when executing a script:

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

The reason may be:

Text editing is written through Notepad or other editors under windows. This file runs in cygwin simulated linux software environment

Solution:

The file format difference between dos and Linux

Text files under DOS take \ R \ n as the line break flag, expressed in hexadecimal, which is 0d 0A. Text files under UNIX take \ n as the line break flag, which is expressed in hexadecimal, which is 0A

[in windows, two symbols for line feed, enter \ R, line feed symbol \ n, only one symbol is required under Linux .]

Text files in DOS format will display ^ m at the end of the line when they are opened with a lower version of VI under Linux. Of course, they may not be visible, but when they are opened with VI, the format of this file will be displayed below, “m.txt” [DOS] 8L, 72C indicates that it is 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