Syntax error near unexpected token ‘$’Do is reported when running bash file

Write a sh file directly on Pycharm and run it on the server to report an error, such as a title.
Then, vim on the server takes all line breaks, whitespaces, and retyping is invalid
Finally, the reasons for the errors and two solutions are summarized.
The reason:
As many blogs have explained, because of the difference between line breaks in Windows and Linux systems, in Windows the line break is ‘\r\n’ whereas in Linux it is ‘\n’, now we understand the meaning of error reporting. For code written in Windows format to be executed on Linux, there is an extra ‘\r’, this is the reason: “Syntax error near token ‘$’do\r'” ”
Visually, run the command and change your file name.

cat -v

There will be:

for i in 1 2 3;^M

If you have this to the M, that means you’re a window newline, and if you’re a Linux newline, you’re not going to have a to the M at the end.
The solution
1. Novice White, simple violence
Cat first:



for i in 1 2 3;

Then select the left key on CMD to copy it, then vim creates a new file, switch to the writing mode (press I), and then right-click to copy once, save and exit.
2. Replace ‘\r’ with sed.

sed 's/\r//' >

The first S means a replacement, which is to replace the /r in with a ‘ ‘and save the replacement to the file
The above

Read More: