Windows中的换行符为CRLF, 即正则表达式的rn(ASCII码为13和10), 而Unix(或Linux)换行符为LF, 即正则表达式的n.
在Windows和Linux下协同工作的时候, 往往这个细小的差别就导致问题, 如
1)Windows下写的Shell脚本, 在Linux下运行时往往出现rn是无效参数, 不能执行;
2)vi 等编器下打开一个文本文件会包含 ^M
3)Linux下查看文件会在文件头部出现一些乱字符
4)在Windows下开发写的Makefile往往也会运行出错.
…
解决的方法:
1) 在进行编辑或保存的时候, 选择行末符号为Unix换行(LF)
2) 文本批量转换工具: Unix系统中的dos2unix(相反的也有unix2dos)
$ dos2unix –help
dos2unix version 0.1.3
converts the line endings of text files from
DOS style (0x0d 0x0a) to UNIX style (0x0a)Usage: dos2unix [OPTION...] [input file list...]
Main options (not all may apply)
-A, –auto Output format will be the opposite of the autodetected source
format
-D, –u2d Output will be in DOS format
–unix2dos Output will be in DOS format
-U, –d2u Output will be in UNIX format
–dos2unix Output will be in UNIX format
–force Ignore binary file detection
–safe Do not modify binary filesHelp options
-?, –help Show this help message
–usage Display brief usage message
–version Display version information
–license Display licensing informationOther arguments
[input file list...] for each file listed, convert in place.
If none specified, then use stdin/stdout
例如, 将当前目录下所有的文本文件转换为Unix格式:
dos2unix -U –safe *
如果二进制文件也进行转换需要用–force参数:
dos2unix -U –force *
评论