shell指令碼報錯:-bash: xxx: /bin/sh^M: bad interpreter: No such fi

來源:互聯網
上載者:User

今天寫了一個shell指令碼,然後在執行的時候報錯,指令碼內容很簡單,僅供測試:
Shell代碼:  #!/bin/sh 
 
echo "test shell " 具體報錯資訊如下:
Shell代碼:  [root@localhost test]# ./test.sh 
-bash: ./test.sh: /bin/sh^M: bad interpreter: No such file or directory 由於之前自己對shell不太熟悉,找同事溝通了後瞭解了原委,主要原因是test.sh是我在windows下編輯然後上傳到linux系統裡執行的。.sh檔案的格式為dos格式。而linux只能執行格式為unix格式的指令碼。
我們可以通過vi編輯器來查看檔案的format格式。步驟如下:
1.首先用vi命令開啟檔案
Shell代碼 : [root@localhost test]# vi test.sh 2.在vi命令模式中使用 :set ff 命令

可以看到改檔案的格式為dos

3.修改檔案format為unix
方法一:使用vi修改檔案format
命令:set ff=unix

執行完後再通過set ff命令查看檔案格式,結果如下:

方法二:直接使用dos2unix命令修改
Shell代碼:  [root@localhost test]# dos2unix test.sh 
dos2unix: converting file test.sh to UNIX format ...  修改完後再次執行./test.sh,執行正確:
Shell代碼:  [root@localhost test]# ./test.sh 
test shell 
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.