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.
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:
- Shell script execution error: “syntax error near unexpected token”
- Shell script syntax error near unexpected token ‘$’Do
- Solve the problem of shell script “syntax error near unexpected token `fi’”.
- Solution to the error $’\ R’: command not found when executing shell script under Linux
- Solution to shell script invocation error during Xcode real machine test
- Shell script: syntax error: bad for loop variable error resolution
- The shell script exits after an error is reported
- Error BC: command not found when git bash runs shell script
- Shell script – EQ – Ne – GT – LT – ge – le
- When executing shell script, $’r ‘: command not found appears
- Syntax error near unexpected token `newline’script cannot be executed
- Shell script running error bash: JPS: command not found
- If elif writing method of shell script
- SCRIPT1002: syntax error File: 0. chunk.js ,SCRIPT5009: ‘Map‘ is undefined File: 0. chunk.js -Solutions
- An undetermined call to function ‘shell’: missing ‘. Stop. Problem encountered when using shell command in makefile
- In Linux shell script, about the commonly used flag [- EQ, GT..] in test and if judgment
- Differences between shell script variable assignment and C language variable assignment
- SH script reports error “Eval: Line 1: syntax error: terminated quoted string”
- [shell] sh executes the script and reports an error syntax error: “(” unexpected “)
- The difference between single equal sign and double equal sign EQ in shell script