修改windows文件的换行符

 应用场景:

在办公中,有可能存在,某些命令脚本使用windows下的文本编辑器进行编写

当放到测试环境的Linux中时,运行报错  

 需要使用的软件:xxd hexdump  dos2unix

 1、运行windows上编写好的sh脚本

[root@hlrgm ~]# bash test.sh
test.sh: line 2: $'
': command not found
'f: invalid option -- '
Try 'df --help' for more information.
test.sh: line 4: $'
': command not found
's: invalid option -- '
Try 'ls --help' for more information.
test.sh: line 6: $'
': command not found  

 2、查看sh脚本

[root@hlrgm ~]# cat test.sh
#!/bin/bash

df -h

ls -l
  

 3、查看文本的二进制

[root@hlrgm ~]# xxd test.sh 
0000000: 2321 2f62 696e 2f62 6173 680d 0a0d 0a64  #!/bin/bash....d
0000010: 6620 2d68 0d0a 0d0a 6c73 202d 6c0d 0a0d  f -h....ls -l...
0000020: 0a                                       .  

 4、查看文本的十六进制

[root@hlrgm ~]# hexdump test.sh
0000000 2123 622f 6e69 622f 7361 0d68 0d0a 640a
0000010 2066 682d 0a0d 0a0d 736c 2d20 0d6c 0d0a
0000020 000a                                   
0000021  

 5、使用dos2unix修改文本

注意:dos2unix需要安装后使用,默认系统未安装

[root@hlrgm ~]# dos2unix test.sh
dos2unix: converting file test.sh to Unix format ...  

 6、查看修改后文件的二进制和十六进制

# 二进制
[root@hlrgm ~]# xxd test.sh 
0000000: 2321 2f62 696e 2f62 6173 680a 0a64 6620  #!/bin/bash..df 
0000010: 2d68 0a0a 6c73 202d 6c0a 0a              -h..ls -l..

# 十六进制
0000000 2123 622f 6e69 622f 7361 0a68 640a 2066
0000010 682d 0a0a 736c 2d20 0a6c 000a          
000001b  

 7、对比

windows:二进制下换行符号:0d0a

linxu:二进制下换行符号:0a  
原文地址:https://www.cnblogs.com/evescn/p/7727268.html