Solution to shell script error “expr: syntax error”

A few days ago, I wrote a shell script, which was normally run on my local test server. In the online server environment, I ran syntax error near token ‘. There was no problem looking at shell script left and right, so I could not Google search. It is as follows:
Open your SHELL script file with the command VI-b, and you will. Notice that each line of script ends up with an extra ^M.
So the next thing I need to figure out is what is this to the M?
long long ago….. The old teletype machine used two characters to start a new line. A character moves the slider backward (called carriage return, < CR> , ASCII code is 0D), another character moves the paper one line (called newline, < LF> , ASCII code is 0A). When computers came along, memory used to be very expensive. Some people decide that it’s unnecessary to use two characters for the end of a line. UNIX developers decided that they could use a single character for the end of a line. Linux follows UNIX, which is also < LF> . Apple developers prescribe the use of < CR> . The guys who developed MS-DOS and Windows decided to stick with the old fashioned. CR> < LF> .
Because MS-DOS and Windows are carriage return + line feed to represent line feed, Vim is used in Linux to view the code written in Windows with VC. The “^M” symbol at the end of the line represents the character.
To solve this problem in Vim, simply use the substitution function in Vim to eliminate “^M” and type the following substitution command line:
1)vi -b setup.sh
2) At command edit line < ESC key and shift+: colon > Input: % s/M ^// g
Note: the “^M” character on the command line above is not “^” plus “M”, but is generated by “Ctrl+ V” and “Ctrl+M”.
After this substitution, the save is ready to be executed. Of course there are other alternatives such as:
A. Some Linux versions have the DOS2UNIX program, which can be used to remove ^M.
b.cat filename1 | tr -d “/r” > Newfile gets rid of ^M to create a newfile, sed commands, etc. Any command that can be replaced can be used to create a newfile.

According to the above mentioned, delete the ^Mshell script and it will work fine. Later, I asked my colleague that he had changed the program path in Windows Notepad, resulting in an extra ^M in each line.

Read More: