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 bb.sh

There will be:

for i in 1 2 3;^M
do^M
  echo"aa"^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:

cat bb.sh

Output:


for i in 1 2 3;
do
  echo"aa"

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//' bb.sh > aa.sh

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

Read More: