在windows和linux系统之间对同一个文件进行交替操作时,常常会遇到一些奇怪的问题,比如无法执行shell脚本或者找不到shell脚本等情况。本文由花神庙码农撰写,将针对这一问题进行总结,供大家参考。
接下来,以名为test-dos.sh的文件为例,详细说明不同操作系统下的换行符格式差异。
#!/bin/bash echo "Hello World !"
[qxhgd@localhost crlf]$ ./test-dos.sh -bash: ./test.sh: /bin/bash^M: bad interpreter: No such file or directory
make[3]: ./mksh: Command not found
[qxhgd@localhost crlf]$ cat -v test-dos.sh #!/bin/bash^M echo "Hello World !"^M
显示Tab:
[qxhgd@localhost crlf]$ cat -T test-dos.sh #!/bin/bash ^Iecho "Hello World !"
[qxhgd@localhost crlf]$ od -c test-dos.sh 0000000 # ! / b i n / b a s h \r \n e c h 0000020 o " H e l l o W o r l d ! 0000040 " 0000041
也可以和cat配合使用:
cat test-dos.sh| od -c
[qxhgd@localhost crlf]$ hexdump -c test-dos.sh 0000000 # ! / b i n / b a s h \r \n e c h 0000010 o " H e l l o W o r l d ! 0000020 " 0000021
状态栏下会显示:
“test-dos.sh” [noeol][dos] 2L, 33B
命令模式下执行set ff:
fileformat=dos
— 首先使用gedit打开文件:
[qxhgd@localhost crlf]$ gedit test-dos.sh
— 搜索\r\n,如果搜索到了就表示是DOS格式:
[qxhgd@localhost crlf]$ dos2unix test-dos.sh dos2unix: converting file test-dos.sh to Unix format ... [qxhgd@localhost crlf]$ dos2unix -n test-dos.sh test-unix.sh dos2unix: converting file test-dos.sh to file test-unix.sh in Unix format ...
[qxhgd@localhost crlf]$ fromdos test-dos.sh
— 转换一个文件:
sed ‘s/^M//’ test-dos.sh> test-unix.sh
— 转换多个文件:
find ./ -type f print0 | xargs -0 sed -i 's/^M$//'
— 1、vi test-dos.sh — 2、:%s/^M//g或:%s/\r//g
— 3、esc退出 :wq保存退出
其中^M 必须是同时按 Ctrl+V+M(按住Ctrl键,然后依次V、M键)或依次按Ctrl + V然后Ctrl + M,表示回车。
tr -d "\015" test-dos.sh cat test-dos.sh|tr -d ‘/r' > test-unix.sh tr -d '\r' test-unix.sh
cat test-dos.sh | perl -pe ‘~s/\r//g’ > test-unix.sh perl -p -e 's/\r//g' test-dos.sh> test-unix.sh perl -pi -e 's/\r\n/\n/g' test-dos.sh
以上就是一文搞清UNIX/Linux与Windows文件换行符格式差异的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号