Shell-shell programming error: "syntax error near unexpected token '"

Source: Internet
Author: User

From: http://blog.csdn.net/xyp84/article/details/4435899

Today, I wrote a shell script, which runs normally on my machine. To my colleagues, run the syntax error near unexpected token error. I can check the shell script on the left and right, but there is no way to search by Google, I found that a senior man spoke quite well. The content is as follows:

Run Command vi-B to open your shell script file. You will. It is found that each row of scripts has an additional ^ m.

Then we need to figure out what this ^ m is?

Long long ago... the old-fashioned typewriter uses two characters to start a new line. One character moves the slide Frame back to the first place (called carriage return, <CR>, ASCII code: 0d), and the other character moves a line (called line feed, <LF>, ASCII code: 0a ). After the advent of computers, memory was once very expensive. Some people think that there is no need to use two characters to represent the end of the line. UNIX
Developers decide that they can use a single character to indicate the end of a line. Linux follows UNIX and is also <LF>. Apple developers require the use of <CR>. Those who develop MS-DOS and Windows decide to follow the old-fashioned <CR> <LF>.

Because MS-DOS and windows are carriage return + line feed to represent the line feed, so in Linux with vim to view the code written with VC in windows, line after the "^ m" symbol, indicates the operator.

To solve this problem in Vim, you can use the replacement function in VIM to kill "^ m" and enter the following command line to replace:

1) vi-B setup. Sh

2) In the command editing line, Press ESC and SHIFT +: Colon> enter: % S/^ m/g.

Note: The "^ m" character in the preceding command line is generated by the "Ctrl + V" and "Ctrl + M" keys instead of "^" and "m.

After replacement, save and run the command. Of course, there are other replacement methods, such:

A. Some Linux versions include the dos2unix program, which can be used to remove ^ m.

B. cat filename1 | tr-d "/R"> remove the newfile ^ m to generate a new file, and use the SED command to generate a new file. All commands that can be replaced can be used to generate a new file.

 

As mentioned above, deleting the ^ mshell script will run normally. Later, I asked my colleague that he had modified the program path in Windows notepad, resulting in ^ m more in each row.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.