解決/bin/bash^M: bad interpreter問題

問題原因

windows環境下 shell腳本文件是dos格式,即每一行結尾以\r\n來標識,而unix格式的文件行尾則以\n來標識

查看腳本文件是dos格式還是unix格式

1、 cat -A filename 從顯示結果可以判斷,dos格式的文件行尾爲^M$,unix格式的文件行尾爲$;

(1)、dos格式

[root@localhost ~]# cat -A test_file.sh

;;^M$
esac^M$

(2)、unix格式

;;$
esac$

2、 od -t x1 filename 如果看到輸出內容中存在0d 0a的字符,那麼文件是dos格式,如果只有0a,則是unix格式;

(1)、dos格式

0066460 70 0d 0a 20 20 65 78 69 74 20 31 0d 0a 20 20 3b
0066500 3b 0d 0a 65 73 61 63 0d 0a

[root@localhost ~]# od -t x1 test_file.sh

(2)、unix格式

0064700 65 6c 70 0a 20 20 65 78 69 74 20 31 0a 20 20 3b
0064720 3b 0a 65 73 61 63 0a

3、 vi filename打開文件,執行 : set ff,如果文件爲dos格式在顯示爲fileformat=dos,如果是unxi則顯示爲fileformat=unix

(1)、dos格式

:set ff

fileformat=dos

[root@localhost ~]# vi test_file.sh

(2)、unix格式

:set ff

fileformat=unix

轉換格式

(1)、使用linux命令dos2unix filename,直接把文件轉換爲unix格式;

(2)、 使用sed命令sed -i “s/\r//” filename 或者 sed -i “s/^M//” filename直接替換結尾符爲unix格式;

(3)、 vi filename打開文件,執行 : set ff=unix 設置文件爲unix,然後執行:wq,保存成unix格式。

查看原文

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章