dos2unix unix2dos se ff=uni問題及解決方案

?在windwos下用記事本編寫了個(gè)shell腳本,拿到cygwin下運(yùn)行沒問題,但是
拿到linux下運(yùn)行就出問題,顯示:?
:bad interpreter:no such file or directory?
出現(xiàn)問題的原因就是“行終束符”,unix/linux使用 換行符;而dos/windows使?
用?回車+換行?
解決辦法:?
1.使用vi打開該腳本?
2.設(shè)置fileformat(ff)?
?:set ff=unix?
3.另存?
?:w test.sh?
此時(shí)test.sh就能在linux下執(zhí)行了(運(yùn)行前注意是否可執(zhí)行)?
注:在cygwin下fileformat為unix或dos都可以?
示例一?DOS格式文本文件在Linux下的表現(xiàn)?
現(xiàn)在有一個(gè)腳本文件job.sh,是在Linux下用vi編輯的。?
[root@jfht ~]# cat job.sh?
#!/bin/sh?
date >job.txt?
現(xiàn)在把它轉(zhuǎn)換成DOS格式文本文件。?
[root@jfht ~]# unix2dos job.sh?
unix2dos: converting file job.sh to DOS format ...?
嘗試著運(yùn)行一下。?
[root@jfht ~]# ./job.sh?
-bash: ./job.sh:?權(quán)限不夠?
[root@jfht ~]# chmod +x job.sh?
[root@jfht ~]# ./job.sh?
-bash: ./job.sh: /bin/sh^M: bad interpreter:?沒有那個(gè)文件或目錄?
DOS格式的腳本文件時(shí)無法解釋執(zhí)行的,因?yàn)槟_本文件的第一行是用來指定解釋?
器的,Linux系統(tǒng)認(rèn)為解釋器是/bin/sh^M,而不是/bin/sh。?
我們來通過Linux下的一些命令來看一下DOS格式文件的真面目。?
[root@jfht ~]# cat -v job.sh ??<== cat -v可以看到文件中的非打印字符?
,而不帶-v參數(shù)的cat命令不行。?
#!/bin/sh^M?
^M?
date >job.txt^M?
^M?
[root@jfht ~]# hexdump -C job.sh ???<== hexdump -C可以看到文件每個(gè)?
字節(jié)的十六進(jìn)制表示。?
00000000 23 21 2f 62 69 6e 2f 73 68 0d 0a 0d 0a 64 61 74 |?
#!/bin/sh....dat|?
00000010 65 20 3e 6a 6f 62 2e 74 78 74 0d 0a 0d 0a ????|e?
>job.txt....|?
0000001e?
[root@jfht ~]# vi job.sh ??<==?使用vi打開時(shí)可以看到底下有[dos]的格?
式提示。有些版本vi顯示的是行尾為^M。?
#!/bin/sh?
date >job.txt?
~ ??????????????????????????????????
???????????????????????????????
~ ???????
"job.sh" [dos ] 4L, 30C?
現(xiàn)在我們把DOS格式改回Unix格式的,看看效果。?
root@jfht ~]# dos2unix job.sh?
dos2unix: converting file job.sh to UNIX format ...?
[root@jfht ~]# ./job.sh?
可以執(zhí)行了,不再報(bào)“-bash: ./job.sh: /bin/sh^M: bad interpreter:?沒有?
那個(gè)文件或目錄”這個(gè)錯(cuò)了。?
[root@jfht ~]#?
示例二?dos2unix -k和dos2unix -n的使用示例?
[root@jfht ~]# cat <<EOF >1.txt?
> 1?
> 2?
> 3?
> EOF?
[root@jfht ~]# file 1.txt?
1.txt: ASCII text?
[root@jfht ~]# ls -l 1.txt?
-rw-r--r-- 1 root root 6 11-14 09:08 1.txt?
[root@jfht ~]# date?
2010年?11月?14日 星期日?09:28:42 CST?
[root@jfht ~]# unix2dos -k 1.txt ??<==?保持文件時(shí)間戳?
unix2dos: converting file 1.txt to DOS format ...?
[root@jfht ~]# ls -l 1.txt?
-rw-r--r-- 1 root root 9 11-14 09:08 1.txt?
[root@jfht ~]# dos2unix -n 1.txt 2.txt ??<==?將1.txt轉(zhuǎn)換到2.txt?
dos2unix: converting file 1.txt to file 2.txt in UNIX format ...?
[root@jfht ~]# ls -l 1.txt 2.txt?
-rw-r--r-- 1 root root 9 11-14 09:08 1.txt?
-rw-r--r-- 1 root root 6 11-14 09:30 2.txt?
[root@jfht ~]# file 1.txt 2.txt?
1.txt: ASCII text, with CRLF line terminators?
2.txt: ASCII text?
[root@jfht ~]# cat -v 1.txt?
1^M?
2^M?
3^M?
[root@jfht ~]# cat -v 2.txt?
1?
2?
3?
[root@jfht ~]#?
?注意:dos2unix?而不是?unix2unix?
?記得用?rpm -qf `which dos2unix`?查看安裝包
了解更多網(wǎng)絡(luò)知識(shí)關(guān)注:http://www.vecloud.com/