標籤:
我們的開發環境一般都使用windows作業系統,而測試環境和線上環境一般使用linux。windows下編輯的shell指令碼,上傳到windows下會發生錯誤。出現兩種情況:
1、BOM頭問題,前面有介紹,不再繼續
2、斷行符號符問題。 主要是在windows作業系統下,採用的編輯器(windows內建的文字編輯器或者zend studio)編輯shell指令碼,在處理換行的時候,一般會附加一個斷行符號符,在linux環境下運行,這個斷行符號符會影響指令碼運行,返回語法錯誤。
我們看一下
首先,我們在windows下編輯一個文本(test00.php),輸入下面的內容,之後上傳到linux伺服器
我們用vi開啟看一下,似乎沒有問題
我們再在linux伺服器上直接寫一個檔案(test11.php)輸入同樣的內容。但是對比一下,我們可以清晰的看見兩個檔案的大小是不一樣的
[[email protected] 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!..i0000010: 7420 6973 2061 2074 6573 7421 0d0a 6974 t is a test!..it0000020: 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!.it0000010: 2069 7320 6120 7465 7374 210a 6974 2069 is a test!.it i0000020: 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命令解決即可
[[email protected] test]# dos2unix test00.php dos2unix: converting file test00.php to UNIX format ...[[email protected] test]# xxd test00.php 0000000: 6974 2069 7320 6120 7465 7374 210a 6974 it is a test!.it0000010: 2069 7320 6120 7465 7374 210a 6974 2069 is a test!.it i0000020: 7320 6120 7465 7374 210a 6974 2069 7320 s a test!.it is 0000030: 6120 7465 7374 21 a test![[email protected] test]#
windows編輯文本和unix編輯文本的斷行符號符問題