关于报错“syntax error near unexpected token `”和回车换行
本来是很简单一个事情,转过来是因为打字机这事比较有趣……
http://blog.csdn.net/xyp84/archive/2009/08/11/4435899.aspx
摘要一下:
|
回车 |
换行 |
|
0D |
0A |
|
<CR> |
<LF> |
|
Carriage Return |
Line Feed |
|
“/r” |
“/n” |
|
|
win32 |
0D |
0D 0A |
|
unix |
0D |
0A |
============淡定的分割线===============
今天写了个shell脚本,在自己机器上运行正常,给同事,运行报错syntax error near unexpected token `,左看右看shell脚本没有问题,没有办法google搜索,发现一位仁兄讲的挺好,内容如下:
用命令vi -b 打开你的SHELL脚本文件,你会。发现每行脚本最后多了个^M。
那么接下来就要搞清楚这个^M是什么东东?
long long ago..... 老式的电传打字机使用两个字符来另起新行。一个字符把滑动架移回首位 (称为回车,<CR>,ASCII码为0D),另一个字符把纸上移一行 (称为换行, <LF>,ASCII码为0A)。当计算机问世以后,存储器曾经非常昂贵。有些人就认定没必要用两个字符来表示行尾。UNIX 开发者决定他们可以用 一个字符来表示行尾,Linux沿袭Unix,也是<LF>。Apple 开发者规定了用<CR>。开发 MS-DOS以及Windows 的那些家伙则决定沿用老式的<CR><LF>。
因为MS-DOS及Windows是回车+换行来表示换行,因此在Linux下用Vim查看在Windows下用VC写的代码,行尾后的“^M”符号,表示的是符。
在Vim中解决这个问题,很简单,在Vim中利用替换功能就可以将“^M”都干掉,键入如下替换命令行:
1)vi -b setup.sh
2)在命令编辑行<就是: 按ESC键 然后shift+:冒号>输入:%s/^M//g
注意:上述命令行中的“^M”符,不是“^”再加上“M”,而是由“Ctrl+v”、“Ctrl+M”键生成的。
这样替换掉以后,保存就可以执行了。当然还有其他的替换方式比如:
a.一些linux版本有 dos2unix 程序,可以用来祛除^M。
b.cat filename1 | tr -d "/r" > newfile 去掉^M生成一个新文件,还有sed命令等,凡是可以替换的命令都是可以用来新生成一个文件的。
按照上面所说的,删除^Mshell脚本就运行正常,后来问同事,原来他windows记事本中修改了程序路径,导致每一行多出了^M。
关于报错“syntax error near unexpected token `”和回车换行的更多相关文章
- Shell脚本报错--syntax error near unexpected token for((i=0;i<$length;i++))
现象: shell脚本使用Nodepad++进行本地编辑,在编辑后上传到linux机器进行执行时提示“syntax error near unexpected token for((i=0;i< ...
- shell编程报错:“syntax error near unexpected token `”
今天写了个shell脚本,在自己机器上运行正常,给同事,运行报错syntax error near unexpected token `,左看右看shell脚本没有问题,没有办法google搜索,发现 ...
- -bash: /etc/profile: line 11: syntax error near unexpected token `$'{\r''报错问题解决
在Linux系统配置Java环境变量之后执行 source /etc/profile指令报:-bash: /etc/profile: line 11: syntax error near unexpe ...
- JS function document.onclick(){}报错Syntax error on token "function", delete this token
JS function document.onclick(){}报错Syntax error on token "function", delete this token func ...
- JS function document.onclick(){}报错Syntax error on token "function", delete this token - CSDN博客
原文:JS function document.onclick(){}报错Syntax error on token "function", delete this token - ...
- Linix登录报"/etc/profile: line 11: syntax error near unexpected token `$'{\r''"
同事反馈他在一测试服务器(CentOS Linux release 7.2.1511)上修改了/etc/profile文件后,使用source命令不能生效,让我帮忙看看,结果使用SecureCRT一登 ...
- libtool: syntax error near unexpected token `]*
../libtool: line 543: syntax error near unexpected token `]*' ../libtool: line 543: ` *[\[\~\#\ ...
- 写shell,运行出错:syntax error near unexpected token `$’do\r”
cygwin下面写shell,运行出错:syntax error near unexpected token `$’do\r” 写shell,运行出错:syntax error near unexpe ...
- Linux中syntax error near unexpected token 错误提示解决方法
Linux中syntax error near unexpected token ... 错误提示有一般有两种原因: 1)window和Linux下换行符不一致导致 window下的换行和Linux下 ...
随机推荐
- C# 怎么显示中文格式的星期几
1.DateTime.Now.ToString("dddd",new System.Globalization.CultureInfo("zh-cn")); 2 ...
- android学习的网站收集
1. http://mob.com/#/index 提供分享等统一解决方案 2. http://bbs.apkbus.com/explore/ 这个类似的quroa问答模块,覆盖不错.就是人气,稍差. ...
- [ES6] 01. Intro to ES6 and traceur compiler
---恢复内容开始--- ES6 is ECMAScript version 6, which JavaScript is based on. The next version of JavaScri ...
- 双硬盘Win7装Ubuntu 12.04经验并解决无线网络不能使用问题
RFKill Many computer systems contain radio transmitters, including Wi-Fi, Bluetooth, and 3G devices. ...
- LeetCode_Path Sum
一.题目 Path Sum My Submissions Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Project Euler:Problem 76 Counting summations
It is possible to write five as a sum in exactly six different ways: 4 + 1 3 + 2 3 + 1 + 1 2 + 2 + 1 ...
- Burning widget
This is a widget that we can see in Nero, K3B, or other CD/DVD burning software. #!/usr/bin/python # ...
- web前端开发文档
- 数据库选型之MySQL(多线程并发)
刘勇 Email: lyssym@sina.com 本博客记录作者在工作与研究中所经历的点滴,一方面给自己的工作与生活留下印记,另一方面若是能对大家有所帮助,则幸甚至哉矣! 简介 鉴于高频中心库 ...
- PHP-汇总CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI
什么是CGI 1.CGI是HTTP协议与其他外部应用程序之间的一个接口标准 2.CGI程序或脚本(CGI程序通过HTTP服务器去执行时, 必须在CGI程序中制定其执行程序的完整路径, 使SHELL能找 ...