windows编辑文本和unix编辑文本的回车符问题

  我们的开发环境一般都使用windows操作系统,而测试环境和线上环境一般使用linux。windows下编辑的shell脚本,上传到windows下会发生错误。出现两种情况:
  1、BOM头问题,前面有介绍,不再继续
  2、回车符问题。 主要是在windows操作系统下,采用的编辑器(windows自带的文本编辑器或者zend studio)编辑shell脚本,在处理换行的时候,一般会附加一个回车符,在linux环境下运行,这个回车符会影响脚本运行,返回语法错误。

  我们看一下
  首先,我们在windows下编辑一个文本(test00.php),输入下面的内容,之后上传到linux服务器

  

  我们用vi打开看一下,似乎没有问题

 

  我们再在linux服务器上直接写一个文件(test11.php)输入同样的内容。但是对比一下,我们可以清晰的看见两个文件的大小是不一样的

[root@localhost test]# ll test*
-rw-rw-r-- 1 root  root     58 Mar 19 18:03 test00.php
-rw-r--r-- 1 root  root     56 Mar 19 18:04 test11.php

 

使用xxd去看一下编码情况,做一下对比:

windows下 linux下
0000000: 6974 2069 7320 6120 7465 7374 210d 0a69  it is a test!..i
0000010: 7420 6973 2061 2074 6573 7421 0d0a 6974  t is a test!..it
0000020: 2069 7320 6120 7465 7374 210d 0a69 7420   is a test!..it 
0000030: 6973 2061 2074 6573 7421                 is a test!

 

0000000: 6974 2069 7320 6120 7465 7374 210a 6974  it is a test!.it
0000010: 2069 7320 6120 7465 7374 210a 6974 2069   is a test!.it i
0000020: 7320 6120 7465 7374 210a 6974 2069 7320  s a test!.it is 
0000030: 6120 7465 7374 210a                      a test!.

 

 

我们清晰的看到,windows下编辑的文件上传到linux服务器后,每一次换行符前面(0a)都存在回车符(0d)。

那么怎么解决这个问题呢
方法很多,可以使用dos2unix filename命令解决即可

[root@localhost test]# dos2unix test00.php 
dos2unix: converting file test00.php to UNIX format ...
[root@localhost test]# xxd test00.php 
0000000: 6974 2069 7320 6120 7465 7374 210a 6974  it is a test!.it
0000010: 2069 7320 6120 7465 7374 210a 6974 2069   is a test!.it i
0000020: 7320 6120 7465 7374 210a 6974 2069 7320  s a test!.it is 
0000030: 6120 7465 7374 21                        a test!
[root@localhost test]# 

 

posted @ 2015-03-19 18:22  壹木人  阅读(250)  评论(0编辑  收藏  举报