Solve syntax error: unexpected end of file or /bin/bash^m: bad interpeneter: no match file or directory

We create shell script files on Windows and upload them to Linux server. When executing shell scripts, the following errors will be reported if there is no accident:
Syntax Error: unexpected end of File or /bin/bash^M: bad Interpreter: No such file or directory

For example:
[[email protected] ~]# sh percona-xtradb-cluster-install. Sh
percona-xtradb-cluster-install. Sh: line 25: syntax error: unexpected end of file
or
Sh
-bash:./ percona-xtradb-cluster-install:./ percona-xtradb-cluster.sh: /bin/bash^M: bad interpreter: No such file or directory

When executing shell scripts, such errors are prompted mainly because shell script files are in DOS format, where each line ends with \r\n and Unix format files end with \n.
Several ways to view a script file in DOS or Unix format:
Method 1
Vi filename or Vim filename opens the file, in DOS format, with a prompt at the bottom [DOS]

If it is in Unix format, there is no prompt at the bottom

Method 2
Vi filename or Vim filename opens the file and executes :set FF, enter. If the file is in DOS format, it will be displayed as FileFormat = DOS; if it is in Unix format, it will be displayed as FileFormat = Unix as shown in the figure below:
, enter
or
Methods 3
Cat -A filename can tell from the display that the DOS file ends with A line to the ^M$and the Unix file ends with A line of $

Solutions:
Method 1
Vi filename or Vim filename opens the file, executes: set FF = Unix setting the file as Unix, then executes: WQ, saving to Unix format.
Method 2
Use the sed command sed – I “s/\r//” filename or SED – I “s/^M//” filename to directly replace the terminators in Unix format.
(sed – I “s/\r//” filename is recommended, as sed – I “s/^M//” filename may become invalid)
 

Read More: