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

Similar Posts: