Bash腳本報錯:“/bin/bash^M: bad interpreter: No such file or directory”

1、問題

github下載一個源碼,運行install腳本時(# ./myinstal_cp)一直報錯“/bin/bash^M: bad interpreter: No such file or directory”,如:

-bash: ./myinstall_cp: /bin/bash^M: bad interpreter: No such file or directory

用bash命令執行(#  bash myinstal_cp),然後提示腳本文件出現很多\r\n的問題,仔細看腳本源碼,邏輯語法都沒有錯誤。

2、分析

仔細定位執行報錯的原因,-bash: bad interpreter,腳本解釋有問題,且bash執行過程中顯示\r\n語法出錯,聯想到windows系統的編輯器是以\r\n換行的,因此,可能腳本是在windows系統編寫的。

3、解決

方式1:vim打開文件,輸入set ff=unix,然後保存文件即可將windows格式腳本替換爲unix,可以直接執行了。

vim myinstall_cp
:set ff=unix 
:wq

方式2:根據bash執行報錯的行數,在linux環境下找到結尾換行刪掉,重新敲入換行。

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