Shell script execution error: “syntax error near unexpected token”
Step 1: execute VI – B test.sh Step 2: replace: Shift +: Colon & gt; enter: S/^ m// g
Today, I wrote a shell script (which was written on windows and moved to Linux). When it was executed, I always reported the following error:
at first, I thought it was a script syntax problem, and I couldn’t execute it. Then I changed it to the simplest one. Later, I searched the Internet for it. It was not a script syntax problem, but a coding problem. There are two steps to solve the problem
Step 1: execute VI – B test.sh
test.sh Is the script to execute
Step 2: replace: Shift +: Colon & gt; enter: S/^ m// g
In the last line mode:
% s/^ m// g code>
^ m code> is not "^" plus "m", but generated by "Ctrl + V" and "Ctrl + m".
The original reference is as follows:
transferred from: https://blog.csdn.net/xyp84/article/details/4435899
long long ago… Old teletypewriters used two characters to start a new line. One character moves the carriage back to the first position (called carriage return, ASCII code 0d), and the other character moves one line on the paper (called line feed, ASCII code 0A). When computers came out, memory used to be very expensive. Some people don't think it's necessary to use two characters at the end of a line. UNIX developers decided that they could use a single character to indicate the end of a line. Linux follows UNIX, too. Apple developers have defined the use of. The guys who developed MS-DOS and windows decided to use the old-fashioned.
Because MS-DOS and windows use carriage return + line feed to indicate line feed, you can use VIM under Linux to view the code written in VC under windows, and the "^ m" symbol at the end of the line.
To solve this problem in VIM is very simple. You can kill all "^ m" by using the replacement function in vim. Type the following replacement command line:
1)vi -b setup.sh
2) On the command line & lt; press ESC, and then shift +: Colon & gt; enter% s/^ m// g
^ m code> character in the above command line is not "^" plus "m", but generated by "Ctrl + V" and "Ctrl + m" keys.
After this replacement, the save can be executed. Of course, there are other alternatives, such as:
a. Some Linux versions have dos2unix programs that can be used to remove ^ M.
b. Cat filename1 | tr - D "/ R" & gt; newfile removes ^ m to generate a new file, as well as sed command. All replaceable commands can be used to generate a new file.
As mentioned above, deleting the
^ mshell code> script will run normally. Later, I asked my colleague that he had modified the program path in his Windows Notepad, resulting in an extra ^ m in each line.
- Solution to shell script error “expr: syntax error”
- Solve the problem of syntax error: unexpected end of file or syntax error near unexpected token ` fi ‘error
- Shell script syntax error near unexpected token ‘$’Do
- Remove M Characters “Control M” or “^M” or “CTRL M” Characters in a UNIX file
- Visual studio code shortcut
- Python error: syntax error: encoding problem: utf8
- Solve syntax error: unexpected end of file or /bin/bash^m: bad interpeneter: no match file or directory
- Solve the problem of shell script “syntax error near unexpected token `fi'”.
- Syntax error near unexpected token `newline’script cannot be executed
- Syntax error near unexpected token ‘$’Do is reported when running bash file
- Syntax error: unexpected end of file problem [two solutions]
- Solving syntax error: unexpected character after line continuation character
- The meaning of escape character is’ ^] ‘prompted by telnet under linux / Unix
- Optimization and upgrade solution for one click deployment without Jenkins under Linux
- Linux command line cursor moving skills
- Chrome browser forces page refresh (no caching)
- Error after JDK reassembly: Error:opening registry key ‘Software/JavaSoft/JRE’
- [Fixed] Syntax error: word unexpected (expecting “)”)
- About Java File.separator
- How to empty a whole line in latex (simple and effective)